【问题标题】:Get Cursor from SimpleCursorAdapter从 SimpleCursorAdapter 获取光标
【发布时间】: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


【解决方案1】:

我认为这是一个旧帖子,但我认为它可以帮助某人。你可以在 onloadFinshed 方法中实现他的步骤

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    adapter.swapCursor(data);
        /*
        data.moveToFirst();

    if (data.getCount() != 0) {
        for (int i = 0; i < data.getCount(); i++) {
            String s = data.getString(data.getColumnIndex(column no));
            data.moveToNext();
        }
    }


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 2011-12-14
    • 2015-04-18
    相关资源
    最近更新 更多