2012-04-18 237 views
0

我無法更改我的列表視圖項目的背景顏色。Android的列表視圖項目更改背景顏色

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.info_layout); 
    ListView listView; 
    listView = (ListView) findViewById(R.id.infolistView); 
    final String[] values = new String[] { "TO1", "TO2", "TO3" }; 
    ArrayAdapter<String> adapter = 
     new ArrayAdapter<String>(this, 
           android.R.layout.simple_list_item_1, 
           android.R.id.text1, 
           values); 
    listView.setAdapter(adapter); 

    // This is not make anything , why ? 
    View vi=adapter.getView(0, null, null); 
    vi.setBackgroundColor(Color.YELLOW); 

listView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 
       //But here is running good 
       view.setBackgroundColor(Color.RED);} 
    }); 

回答

0

您必須創建自定義適配器才能更改項目的背景顏色。

這裏是定義適配器的例子:

public class PaListAdapter extends BaseAdapter{ 
     private LayoutInflater mInflater; 

     private ArrayList<String> platevalue = new ArrayList<String>(); 

      ViewHolder holder; 
     public PaListAdapter(Context context,ArrayList<String> value) 
     { 
      // Cache the LayoutInflate to avoid asking for a new one each time. 
      mInflater = LayoutInflater.from(context); 



      //mycontext = context; 
      platevalue.clear(); 
      platevalue =value; 



     } 


     public int getCount() 
     { 
      return platevalue.size(); 
     } 

     public Object getItem(int position) 
     { 
      return position; 
     } 

     public long getItemId(int position) 
     { 
      return position; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) 
     { 





      if (convertView == null) 
      { 
       convertView = mInflater.inflate(R.layout.select_dialog, null); 

       holder = new ViewHolder(); 
       holder.hTransID =(TextView) convertView.findViewById(R.id.txtChoice); 

       holder.ch =(CheckBox) convertView.findViewById(R.id.chk); 


       convertView.setTag(holder); 
      } 
      else 
      { 
       holder = (ViewHolder) convertView.getTag(); 
      } 

      holder.hTransID.setText(platevalue.get(position)); 




      return convertView; 
     } 

     static class ViewHolder 
     {  
       TextView hTransID; 


     } 
    } 


select_dialog.xml 


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:descendantFocusability="blocksDescendants" 
android:background="#000000" 
    > 

    <TextView 
     android:id="@+id/txtChoice" 

     android:layout_gravity="center_vertical|left" 
     android:gravity="center_vertical|left" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#000000"/> 

</LinearLayout> 

在活動Class.Define它想:

simpleefficientadapter efficientadapter; 

    efficientadapter=new simpleefficientadapter(CLASSNAME.this, VALUES); 
    listView.setAdapter(efficientadapter); 
+0

當你想改變背景顏色? – 2012-04-19 07:58:07

+0

我已經解決了這個問題 – kosapeve 2012-04-19 16:19:56

1

你正在做的方式只會越來越充滿了位置的數據,然後死了,是該方法被調用後收集垃圾的視圖的一個實例。

要更改列表中的實際視圖,您必須更改適配器的顏色並編寫自定義適配器,或者讓顏色從該狀態開始。

基本上getView不從視圖列表中取出,而是創建一個新視圖,除非您傳遞視圖。

相關問題