迭代 Sqlite-query 中的行
我有一个表布局,我想用数据库查询的结果填充它。我使用全选,查询返回四行数据。
我使用此代码填充表行内的 TextView。
Cursor c = null;
c = dh.getAlternative2();
startManagingCursor(c);
// the desired columns to be bound
String[] columns = new String[] {DataHelper.KEY_ALT};
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.name_entry};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
R.layout.list_example_entry, c, columns, to);
this.setListAdapter(mAdapter);
我希望能够分离 KEY_ALT 的四个不同值,并选择它们的去向。我希望它们填充四个不同的 TextView,而不是上面示例中的一个。
如何迭代生成的游标?
I have a table layout that I want to populate with the result from a database query. I use a select all and the query returns four rows of data.
I use this code to populate the TextViews inside the table rows.
Cursor c = null;
c = dh.getAlternative2();
startManagingCursor(c);
// the desired columns to be bound
String[] columns = new String[] {DataHelper.KEY_ALT};
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.name_entry};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
R.layout.list_example_entry, c, columns, to);
this.setListAdapter(mAdapter);
I want to be able to separate the four different values of KEY_ALT, and choose where they go. I want them to populate four different TextViews instead of one in my example above.
How can I iterate through the resulting cursor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
数据库查询返回的
Cursor
对象位于第一个条目之前,因此迭代可以简化为:引用自
SQLiteDatabase
。Cursor
objects returned by database queries are positioned before the first entry, therefore iteration can be simplified to:Reference from
SQLiteDatabase
.您可以使用下面的代码来遍历光标并将它们存储在字符串数组中,然后您可以将它们设置在四个文本视图中
You can use below code to go through cursor and store them in string array and after you can set them in four textview
迭代可以通过以下方式完成:
Iteration can be done in the following manner:
找到了一种非常简单的方法来迭代游标
Found a very simple way to iterate over a cursor
我同意chiranjib,我的代码如下:
I agree to chiranjib, my code is as follow:
注意:要使用 SQLiteQueryBuilder(),您需要添加
编译 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'
在你的成绩档案中
NOTE: to use SQLiteQueryBuilder() you need to add
compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'
in your grade file