设置 ListView 中Item的背景色

  • public class MusicListAdapter extends BaseAdapter {
  •         MusicList mList;
  •         Context mContext;
  •         int currentIndex=0;
  •         
  •         public MusicListAdapter(Context c,MusicList list)
  •         {
  •                 mList=list;
  •                 mContext=c;
  •         }
  •         public int getCount() {
  •                
  •                 return mList.getSize();
  •         }
  •         public Object getItem(int position) {
  •                
  •                 return mList.getMusicList().get(position);
  •         }
  •         public long getItemId(int position) {
  •                
  •                 return position;
  •         }
  •         public View getView(int position, View convertView, ViewGroup parent) {
  •                
  •                 TextView musicView=new TextView(mContext);
  •                 Music music=(Music)getItem(position);
  •                 String titleString=music.getTagInfo().title;
  •                 if (titleString.contentEquals("")) {
  •                         titleString=music.getPath();
  •                 }
  •                 musicView.setText(titleString+" " +music.getTagInfo().artist);
  •                 if(position==currentIndex)
  •                 {
  •                         musicView.setTextColor(Color.RED);
  •                 }
  •                
  •                 return musicView;
  •         }
  •         
  •         public void setCurrentIndex(int index)
  •         {
  •                 currentIndex=index;
  •         }
  • }

 

设置listview的setOnItemClickListener,在onItemClick中更改你当前获取焦点的item的索引值,调用Adapter的notifyDataSetChanged()函数通知数据的更改以更新UI。

posted @ 2012-05-16 11:04  叁柒  阅读(390)  评论(0编辑  收藏  举报