【发布时间】:2011-03-09 06:22:19
【问题描述】:
我有一个扩展 ListActivity 的类,并包含一个 ListView,它通过对象的 ArrayList 和我创建的自定义适配器填充,该适配器是 ArrayAdapter 的子类。
我想要对象的一个字段来引用是否应该选中或取消选中对象行。
然后,当 ListView 被填充时,一些行将被预先选择(选中)
我已经覆盖了 ArrayAdapter.getView() 以便我可以通过一个对象来填充我的布局。
我以为我会在这里设置要检查或不检查的行 - 但我无法提出解决方案 - 有什么想法吗?
这是我的 getView() 代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.contactedit_onerow, null);
}
ContactVO mObject = items.get(position);
if (mObject != null) {
CheckedTextView nameV = (CheckedTextView) convertView.findViewById(R.id.rowText1);
TextView phoneV = (TextView) convertView.findViewById(R.id.rowText2);
TextView emailV = (TextView) convertView.findViewById(R.id.rowText3);
TextView headerV = (TextView) convertView.findViewById(R.id.alphaHeader);
if (alphaIndexer.get(mObject.name.substring(0, 1).toUpperCase()) == position ) {
headerV.setText(mObject.name.substring(0,1));
headerV.setVisibility(View.VISIBLE);
} else {
headerV.setText("");
headerV.setVisibility(View.GONE);
}
nameV.setText(mObject.name);
phoneV.setText("Phone: " + mObject.phone);
emailV.setText("Email: " + (mObject.email != null ? mObject.email : ""));
// THE FOLLOWING LINE UNCOMMENTED DOES NOTHING
// (any suggestions to make it work)
// nameV.setChecked(true);
}
return convertView;
}
提前致谢!
【问题讨论】:
-
我仍然很好奇是否有一种方法可以预先选择(检查)我的 ListView 中的项目,而无需重新遍历数据源(请参阅下面的自我建议的解决方案) - 提前感谢任何人都有任何指针!
标签: android