从 SimpleCursorAdapter 访问 SQLite 值
我有一个名为“Nodes”的 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 中的 position 参数是本地的到子集,所以我无法将它与节点表交叉引用。我是否缺少一些简单的技巧?
一如既往,任何帮助表示赞赏。
标记答案的评论4帮助我解决了这个问题: 将传递给 SimpleCursorAdapter 的光标存储为成员变量,然后使用 move toposition 和 getLong:
mCursor_cur.moveToPosition(position);
long id = mCursor_cur.getLong(0);
其中 position 是 getView() 方法中的本地参数,0 用于 getLong,因为db_id 是传递给 SimpleCursorAdapter 的游标中的第一列
I have a SQLite table called "Nodes" with a set of all nodes (each node has multiple properties).
In an activity, I use an extension of SimpleCursorAdapter to display a subset of the Nodes table, based on a SQL query, passing it the Cursor over the table of results (from the query).
{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"
I want to modify each of the list items in my ListActivity such that it displays a different color based on whether an item is cool, warm or hot. I have a small (empty) view, but it's always defaulting to white (the 'else' portion).
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);
}
My issue is that while the cursor passed to the SimpleListAdapter will contain the _IDs (I assume) of the corresponding nodes in the subset I want, the position parameter in getView is local to the subset, so I can't cross-reference it with the Nodes table. Am I missing some easy technique?
As always, any help appreciated.
comment 4 of the marked answer helped me solve this:
storing the cursor that was passed to the SimpleCursorAdapter as a member variable, then using move to position, and getLong:
mCursor_cur.moveToPosition(position);
long id = mCursor_cur.getLong(0);
where position is the local parameter in the getView() method, and 0 is used for getLong because the db_id is the first column in the cursor passed to the SimpleCursorAdapter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在java中==不应该用于字符串比较
string1.equals(string2)
是您正在寻找的请参阅此处了解更多信息
in java == should not be used for string comparison
string1.equals(string2)
is what your are looking forsee here for further info