0
我正在从我的数据库中的数据编译的数组创建一个列表视图。 数组1:firstnamearray(由名字组成) 数组2:lastnamearray(由姓氏组成)使特定的listview行背景不同的颜色 - Android?
每个listview行都会在其中列出一个名称。例如,如果名字是“约瑟夫”,我希望该行的背景为绿色。我将如何做到这一点?
这里是我的代码:
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”)。
这将工作与滚动?我读了一些关于视图位置随着滚动和有问题的人而变化的问题,因为背景颜色转移到不同的列表视图行 – user3794585 2014-09-03 00:58:55
@ user3794585当我扩展基础适配器时,它没有工作,但是您正在扩展阵列适配器,因此它可能工作。使它工作的傻瓜证明方法是添加一个else子句。看到我编辑的答案。 – 2014-09-03 01:01:46
谢谢你应该工作。不能相信我忽略了这个 – user3794585 2014-09-03 01:10:18