我有一个名为“节点”的 SQLite 表,其中包含一组所有节点(每个节点都有多个属性)。
在一个活动中,我使用 SimpleCursorAdapter 的扩展来显示 Nodes 表的子集,基于 SQL 查询,将 Cursor 传递给结果表(来自查询)。
{id_long, name_string, description_string, nodetype_enum_toString...}
1, "home", "home_desc", "cool"
2, "item1", "item1_desc", "warm"
3, "item2", "item2_desc", "hot"
4, "item3", "item3_desc", "warm"
5, "item4", "item4_desc", "hot"
我想修改 ListActivity 中的每个列表项,使其根据项目是冷、暖还是热显示不同的颜色。我有一个小(空)视图,但它始终默认为白色(“其他”部分)。
public View getView(int position, View convertView, ViewGroup parent) {
convertView = super.getView(position, convertView, parent);
if (convertView == null) convertView = View.inflate(mContext, R.layout.viewstack_item, null);
View nodetype_view = (View) convertView.findViewById(R.id.nodetype_view);
mGenCursor_cur = mDataHelper_db.getAll();
mGenCursor_cur.moveToPosition(position);
Log.i("test", "node type key " + mGenCursor_cur.getColumnIndex(DataHelper.NODES_TYPE_KEY));
String type_nt = mGenCursor_cur.getString(mGenCursor_cur.getColumnIndex(DataHelper.NODES_TYPE_KEY));
String name_str = mGenCursor_cur.getString(mGenCursor_cur.getColumnIndex(DataHelper.NODES_NAME_KEY));
Log.i("getView", title_str + "typeis " + type_nt + " at position " + position);
if (prio_np.equals(NodePrio.HIGH.toString())) {
Log.i("prio_np", "high red");
prioView_view.setBackgroundColor(Color.RED);
} else if (prio_np.equals(NodePrio.NORMAL.toString())) {
Log.i("prio_np", "norm magenta");
prioView_view.setBackgroundColor(Color.MAGENTA);
} else {
Log.i("prio_np", "else (low) white");
prioView_view.setBackgroundColor(Color.WHITE);
}
mGenCursor_cur.close();
bindView(convertView, mContext, getCursor());
return(convertView);
}
我的问题是,虽然传递给 SimpleListAdapter 的光标将包含我想要的子集中相应节点的 _ID(我假设),但getView中的位置参数是子集的本地参数,所以我不能交叉引用它节点表。我错过了一些简单的技术吗?
一如既往,任何帮助表示赞赏。
标记答案的注释 4 帮助我解决了这个问题:将传递给 SimpleCursorAdapter 的光标存储为成员变量,然后使用移动到位置和 getLong:
mCursor_cur.moveToPosition(position);
long id = mCursor_cur.getLong(0);
其中position是 getView() 方法中的本地参数,而 0 用于 getLong 因为 db_id 是传递给 SimpleCursorAdapter 的游标中的第一列