我正在尝试根据内容来绘制行ListView
。
我有一个int数组 - int[] State= new int[] {1,1,1,1,1,1,0,0,0,0};
我需要根据数组中的值绘制红色或绿色的每一行。绿色标志为0,红色标志为1。
例如:int[] State= new int[] {1,1,1,1,1,1,0,0,0,0};
编号为0-5的行必须为红色,5-9必须为红色。
我有适配器:
public class SpecialAdapter extends SimpleAdapter {
private int[] colors = new int[] {Color.RED, Color.GREEN };
private int[] State= new int[] {1,1,1,1,1,1,0,0,0,0};
public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
///It makes my list red-green, like zebra, but I need to paint depending on flags in State
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}
}
在主要活动中:
SpecialAdapter adapter = new SpecialAdapter(getBaseContext(), myArrList, R.layout.list_item,
new String[] {"State", "Name"},
new int[] {R.id.text1, R.id.text2});
ListView lvMain = (ListView)findViewById(R.id.listView1);
lvMain.setAdapter(adapter);
答案 0 :(得分:0)
您应该更改getView方法。 int colorPos = position % colors.length;
返回“0”或“1”。所以colors[colorPos]
按顺序返回红色和绿色。请试试这个;
public class SpecialAdapter extends SimpleAdapter {
private int[] colors = new int[] {Color.RED, Color.GREEN };
private int[] State= new int[] {1,1,1,1,1,1,0,0,0,0};
public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.setBackgroundColor(colors[State[position]]);
return view;
}
}