Android - 如何使用一条记录(光标)填充ListView中多个列表项的数据?
有谁知道如何实现以下要求?
查询数据库时,只返回一条记录。 该记录将用于填充 ListView 中的几个列表项(例如 5 个条目)。
由于只有一条记录,我在填充第二到第五条记录时遇到困难。
public class TestCursorAdapter extends CursorAdapter {
List list = new ArrayList();
public TestCursorAdapter (Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
list.add("Test 1");
list.add("Test 2");
list.add("Test 3");
list.add("Test 4");
list.add("Test 5");
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// cursor will only return one record, but it needs to populate five records (list size is 5)
}
public void bindView(View view, Context context, Cursor cursor) {
// cursor will only return one record, but it needs to populate five records (list size is 5)
}
public int getCount() {
return list.size();
}
public int getViewTypeCount() {
return 5;
}
}
我正在使用 CursorLoader 从数据库查询数据。
谢谢
Does anyone know how to achieve the below requirement?
When the database is queried, there is only one record returned.
This record will be used to populate few list item in ListView (say 5 entries).
Since it's only one record, I am facing difficulties to populate entries for second up to fifth entries.
public class TestCursorAdapter extends CursorAdapter {
List list = new ArrayList();
public TestCursorAdapter (Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
list.add("Test 1");
list.add("Test 2");
list.add("Test 3");
list.add("Test 4");
list.add("Test 5");
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// cursor will only return one record, but it needs to populate five records (list size is 5)
}
public void bindView(View view, Context context, Cursor cursor) {
// cursor will only return one record, but it needs to populate five records (list size is 5)
}
public int getCount() {
return list.size();
}
public int getViewTypeCount() {
return 5;
}
}
I am using CursorLoader
to query data from database.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CursorAdapter
仅适用于特定格式的Cursor
,在ListView
中每行一行。您最好编写自己的自定义扩展BaseAdapter
其支持数据是您唯一的光标
。然后您可以准确指定如何获取getView()
。CursorAdapter
is only suited toCursors
of a particular format, with one row per row in theListView
. You'd be better off writing your own custom extension ofBaseAdapter
whose backing data is your uniqueCursor
. Then you can specify exactly how to get the data for each position ingetView()
.