【发布时间】:2014-09-03 00:51:29
【问题描述】:
我正在从数据库中的数据编译的数组创建一个列表视图。 数组 1:firstnamearray(由名字组成) 数组2:lastnamearray(由姓氏组成)
每个列表视图行都会列出一个名称。例如,如果名字是“Joseph”,我希望该行的背景为绿色。我该怎么做?
这是我的代码:
CustomList adapter = new CustomList(getActivity(), firstnamearray, lastnamearray);
mainListView.setAdapter(adapter);
这是我的 CustomList 适配器:
public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] firstnamearray;
private final String[] lastnamearray;
public CustomList(Activity context,
String[] firstnamearray, String[] lastnamearray) {
super(context, R.layout.simplerow, firstnamearray);
this.context = context;
this.firstnamearray = firstnamearray;
this.lastnamearray = lastnamearray;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.simplerow, null, true);
TextView firstname = (TextView) rowView.findViewById(R.id.firstname);
TextView lastname = (TextView) rowView.findViewById(R.id.lastname);
cardnumber.setText(firstnamearray[position]);
expdate.setText(lastnamearray[position]);
return rowView;
}
}
我对此进行了研究,并看到一些人谈论在单击或选择时更改背景颜色,但我希望它在创建时具有不同的行颜色(如果名字为“Joseph”)。
【问题讨论】: