2012-08-07 93 views
0

试图通过验证条件来显示基于颜色代码的列表(if(vehicleColor [i] .equals(“1”)))等等。如果我正在关闭循环,代码使用此代码段(getListView()。setBackgroundColor(Color.WHITE);)使用单一颜色工作正常。我需要根据颜色代码显示列表。需要帮助的人。Android:列表样式

私人无效showColouredList(){

final ListAdapter adapter = new ArrayAdapter<String>(this, 
      R.layout.list_items, R.id.label, vehicleList); 
    setListAdapter(adapter); 
    getListView().setTextFilterEnabled(true); 
    getListView().setDividerHeight(1); 
    // getListView().setBackgroundColor(Color.WHITE); 

    for (int i = 0; i < vehicleList.length; i++) { 

     System.out.println("--" + vehicleColor[i]); 
     if (vehicleColor[i].equals("1")) { 
      System.out.println("ListColour"); 

      getListView().getChildAt(i).setBackgroundColor(Color.GREEN); // giving NullPointerException 

     } else if (vehicleColor[i].equals("2")) { 

      getListView().getChildAt(i).setBackgroundColor(Color.RED); 
     } else if (vehicleColor[i].equals("3")) { 

      getListView().getChildAt(i).setBackgroundColor(Color.YELLOW); 
     } else if (vehicleColor[i].equals("4")) { 

      getListView().getChildAt(i).setBackgroundColor(Color.GRAY); 

     } 

    } 

    getListView().setPadding(5, 10, 0, 10); 
    getListView().setCacheColorHint(Color.BLUE); 
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    getListView().setEnabled(true); 

    getListView().setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int index, 
       long arg3) { 
      // TODO Auto-generated method stub 

      Intent secondActivity = new Intent(First.this, 
        SecondActivity.class); 
      Utility.setDeviceId(listBean.getVehicleDeviceIdArr()[index]); 
      Utility.setWindowTitle(list[index]); 
      startActivity(secondActivity); 

     } 
    }); 

} 

回答

0

您可以使用listAdapter,并在getView做你的工作(); List(Array)适配器的方法。

+0

我已经使用了列表适配器,但并不完全知道如何在这里使用getView()方法。我在Android方面经验不足。你能否解释一下或者提供示例代码。 – techniqez 2012-08-07 11:02:09