【发布时间】:2013-05-23 19:13:06
【问题描述】:
我有一个 ListView,我想根据
ArrayList<Integer> 如果项目的位置存在于数组中,则该项目的背景将为绿色,否则应为红色。我使用SetListColor 来做到这一点,但它不起作用。
public class createtarget extends ListActivity
{
String [] Target;
ListView lstView;
public static ArrayList<Integer> coloredItems;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.createtarget);
coloredItems = new ArrayList<Integer>();
coloredItems.add(1);
lstView = getListView();
lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lstView.setTextFilterEnabled(true);
SetListColor(this.findViewById(android.R.id.content)); // Get View of ListView
Target=new String []{"A","B","C"};
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, Target));
}
public void SetListColor(View v)
{
for(int i=0;i<lstView.getCount();i++)
{
System.out.println("Item is: "+i);
if(createtarget.coloredItems.contains(i))
v.setBackgroundColor(Color.GREEN);
else
v.setBackgroundColor(Color.Red);
}
}
【问题讨论】: