2012-04-20 6 views
2

私はの中でプロジェクトをやっています。です。 backgroundの色と同様にtextcolorをリストビューから変更したいと思います。ここだから、私はいくつかの学生の名前でとcheckboxを使用して、複数の選択肢の施設でListViewを持っている私のコード実行時にアンドロイドのListViewの背景色を変更します

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="right" 
    android:orientation="vertical" > 


    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 


     <ListView 
      android:id="@+id/listView1" 
      android:layout_width="265dp" 
      android:layout_height="366dp" 
      android:layout_marginLeft="20dp" 
      android:layout_marginTop="20dp" 
      android:layout_weight="0.00" 
      android:drawSelectorOnTop="true" > 

     </ListView> 
    </LinearLayout> 

</LinearLayout> 

です。

ListView stud_lst=(ListView) findViewById(R.id.listView1); 

stud_lst.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

選択した生徒の背景とテキストの色を変更したいと思います。 私はすでにいくつかの答えを見ましたが、私はそれを得ていません。 私を助けてください。

+0

listview.onclickはカスタム行の背景色とテキスト色を変更します。 –

+0

カスタム行を使用しない他のアイデア –

答えて

-1

アイテムの背景色を変更するには、カスタムアダプタを作成する必要があります。ここでは、カスタムアダプタの例です:

活動Class.Defineで
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); 




       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> 

それが好き:

simpleefficientadapter efficientadapter; 

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

カスタムアダプタを使用して、アクティビティクラスで行います次のようになります。

// mListview is ur listview object. 
    mListview.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
        view.setBackgroundColor("your bg's color id"); 
      } 
    } 
関連する問題