我已经使用 BaseAdapter 准备了一个自定义列表视图。现在我想在单击事件上更改列表视图的选定项目的颜色。并且应该选择多个项目。这里我给出一个演示:
所选项目的颜色为橙色。这只是一个演示屏幕。如果有人知道如何更改所选列表项的整个背景颜色,请发表他们的评论。谢谢。
我正在使用这个 BaseAdapter 类:
public class MyListAdapter extends BaseAdapter {
private Activity activity;
private String[] title, artist, duration, rowNumber;
private static LayoutInflater inflater=null;
ViewHolder holder;
View vi;
public MyListAdapter (Activity context, String[] songTitle,String[] songArtist, String[] songDuration )
{
try
{
activity = context;
title = songTitle;
artist = songArtist;
duration = songDuration;
rowNumber = new String[title.length];
for(int i=0;i<title.length; i++){
rowNumber[i] = Integer.toString(i+1);
}
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
catch (NullPointerException e)
{
e.printStackTrace();
}
}
public int getCount() {
return title.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
private TextView txtSongNumber, txtSongTitle, txtSongArtist, txtSongDuration;
}
public View getView(int position, View convertView, ViewGroup parent)
{
try
{
vi=convertView;
// System.out.println("Value of position"+position);
if(convertView==null)
{
vi = inflater.inflate(R.layout.list_songs, null);
holder=new ViewHolder();
holder.txtSongNumber = (TextView)vi.findViewById(R.id.txtSongNumber);
holder.txtSongTitle = (TextView)vi.findViewById(R.id.txtSongTitle);
holder.txtSongArtist = (TextView)vi.findViewById(R.id.txtSongArtist);
holder.txtSongDuration = (TextView)vi.findViewById(R.id.txtSongDuration);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.txtSongNumber.setText(rowNumber[position]);
holder.txtSongTitle.setText(title[position]);
holder.txtSongArtist.setText(artist[position]);
holder.txtSongDuration.setText(duration[position]);
}
catch (Exception e)
{
e.printStackTrace();
}
return vi;
}
}