【发布时间】:2016-10-10 22:55:06
【问题描述】:
我有一个实现 LoaderManager.LoaderCallbacks 游标类的活动。 我有这个从 SQlite 获取数据的函数。
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
switch (id) {
case ITEM_LOADER_ID:
// The uri points to Item, which is the items in the inventory
Uri uri = InventoryContract.Item.contentUriWithAccount(mCloverAccount);
String sortOrder = InventoryContract.Item.NAME;
String selection = "";
try {
selection = InventoryContract.ItemColumns.CODE;
catch (Exception e){
Log.e("Error",e.toString());
}
return new CursorLoader(HomeActivity.this, uri, null, selection, null, sortOrder);
default:
// An invalid id was passed in
}
throw new IllegalArgumentException("Unknown Loader ID");
}
我有一个 SimpleCursorAdapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_2,
null,
new String[]{InventoryContract.Item.NAME, InventoryContract.Item.PRICE_TYPE},
new int[]{android.R.id.text1, android.R.id.text2},
0
);
这是功能并获取数据,但我想获取光标,以便我可以检索所有信息并存储到 List 以供后期执行。我搜索所有相关链接以获取光标并找到一个代码:
Cursor c = adapter.getCursor();
try {
int i = 0;
while (c.moveToNext()){
Log.e("In Log Cursor",c.getString(i));
i++;
}
c.close();
}
catch (Exception e){
Log.e("Error",e.toString());
}
但它正在获取空游标。我想获取该数据并制作自定义适配器以进行进一步操作。请帮助我找到解决方案或任何其他方式从 SQlite 获取该数据并存储为列表,我可以在获取后使用它并可以在 RecyclerView 中设置它。提前致谢。
【问题讨论】:
-
您不需要任何自定义适配器,只需使用
SimpleCursorAdapter,如果您想在RecyclerView上显示您的数据,请改用this 适配器 -
CursorRecyclerAdapter(Cursor cursor) 我必须在这个函数中传递光标,但是该光标从哪里得到?
标签: android sqlite android-recyclerview simplecursoradapter clover