【问题标题】:Getting error while launching new Activity for Updating the database of recyclerview启动更新recyclerview数据库的新活动时出错
【发布时间】:2019-11-06 09:46:52
【问题描述】:

我正在尝试在下一个活动中传输一些详细信息,但在启动新 Intent 时出现错误 错误:

由 android.database.CursorIndexOutOfBoundsException 引起:请求索引 -1,大小为 1 在 android.database.AbstractCursor.checkPosition(AbstractCursor.java:468) 在 android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 在 android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) 在 android.database.CursorWrapper.getString(CursorWrapper.java:137) 在 com.shashankesh.ms.ItemIncrsDecrs.onCreate(ItemIncrsDecrs.java:42) 在 android.app.Activity.performCreate(Activity.java:7032) 在 android.app.Activity.performCreate(Activity.java:7023) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1236) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)

我从中启动的父 Activity 是:

@Override
public void onClick(int Id) {
    Toast.makeText(this,String.format("ID: "+Id),Toast.LENGTH_LONG).show();
    Intent startInt = new Intent(ItemActivity.this,ItemIncrsDecrs.class);
    startInt.putExtra("ID",Id);
    startActivity(startInt);

}

这是我正在启动的活动

public class ItemIncrsDecrs extends AppCompatActivity {
private Toolbar toolbar;
private SQLiteDatabase mDb;
private int mId;
private String mItemName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_incrs_decrs);
    toolbar = (Toolbar) findViewById(R.id.incrs_decrs_toolbar);
    setSupportActionBar(toolbar);


    // Create a DB helper (this will create the DB if run for the first time)
    ItemDbHelper dbHelper = new ItemDbHelper(this);
    // Keep a reference to the mDb until paused or killed. Get a writable database
    // because you will be adding restaurant customers
    Intent intentThatStartedThisActivity = getIntent();

    if (intentThatStartedThisActivity != null) {
        if (intentThatStartedThisActivity.hasExtra("ID")) {
            mId = intentThatStartedThisActivity.getIntExtra("ID",0);
        }
    }
    mDb = dbHelper.getWritableDatabase();
    Cursor cursor = queryItemName();
    mItemName = cursor.getString(cursor.getColumnIndex(ItemContract.ItemEntry.COLUMN_ITEM_NAME));
    getSupportActionBar().setTitle("Update Item: " + mItemName);
    //updateItem();
}

private Cursor queryItemName() {
    String[] columns = {ItemContract.ItemEntry.COLUMN_ITEM_NAME};
    String selection = ItemContract.ItemEntry._ID + " = ?";
    String[] selectionArgs = {String.valueOf(mId)};
    String groupBy = null;
    String having = null;
    String orderBy = ItemContract.ItemEntry.COLUMN_TIMESTAMP;
    return getContentResolver().query(
            ItemContract.ItemEntry.CONTENT_URI,
            null,
            selection,
            selectionArgs,
            ItemContract.ItemEntry.COLUMN_TIMESTAMP
    );
}

我正在启动上述活动以实现在新活动中更新项目的值,并在它之后返回父活动。

对于内容提供者,我正在使用此代码

@Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection,
                    String[] selectionArgs, String sortOrder) {

    // Get access to underlying database (read-only for query)
    final SQLiteDatabase db = mItemDbHelper.getReadableDatabase();

    // Write URI match code and set a variable to return a Cursor
    int match = sUriMatcher.match(uri);
    Cursor retCursor;

    //  Query for the tasks directory and write a default case
    switch (match) {
        // Query for the tasks directory
        case TASKS:
            retCursor =  db.query(TABLE_NAME,
                    projection,
                    selection,
                    selectionArgs,
                    null,
                    null,
                    sortOrder);
            break;
        // Default exception
        default:
            throw new UnsupportedOperationException("Unknown uri: " + uri);
    }

    //  Set a notification URI on the Cursor and return that Cursor
    retCursor.setNotificationUri(getContext().getContentResolver(), uri);

    // Return the desired Cursor
    return retCursor;
}

请帮我解决上述错误

【问题讨论】:

    标签: android android-sqlite android-contentprovider


    【解决方案1】:

    您需要在从中检索任何数据之前调用 cursor.movetofirst()。所以在你的行之前打电话mItemName = cursor.getString(cursor.getColumnIndex(ItemContract.ItemEntry.COLUMN_ITEM_NAME));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-14
      • 2016-07-07
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 2018-02-19
      相关资源
      最近更新 更多