2012-08-07 96 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