0

I want to return the link from sqlite db by clicking on recyclerview item. It`s always returns the last added. Problem is in cursor, but I don't know,how to fix it.

cursor query in DBManager.class

  public Cursor linkColumn() {
    String[] linkColumn = new String[]{DatabaseHelper.LINK};
    Cursor cursor = sqLiteDatabase
            .query(
                    DatabaseHelper.TABLE_NAME,
                    linkColumn,
                    null,
                    null,
                    null,
                    null,
                    null);
    return cursor;
}

clicking on item

 @Override
        public void onItemClick(long _id) {
            Intent returnIntent = new Intent();
            Cursor cursor = dbManager.linkColumn();

            String item = cursor.getString(cursor.getColumnIndex(DatabaseHelper.LINK));
            returnIntent.putExtra("link", item);
            setResult(RESULT_OK, returnIntent);
            finish();

        }
androiddev
  • 35
  • 7
  • as I said in the exact same question you wrote yesterday and recreate today, **you never tell the cursor which row to take!** [so please look this SO post](http://stackoverflow.com/questions/1243199/how-to-perform-an-sqlite-query-within-an-android-application), and [this official guide](https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html) or [this other SO link](http://stackoverflow.com/questions/10600670/sqlitedatabase-query-method) and finally [a guide on sql](http://www.w3schools.com/sql/) – Pier Giorgio Misley Dec 22 '16 at 11:09
  • @PierGiorgioMisley, I really can't understand, how to return current item, not all column. Please, help. – androiddev Dec 22 '16 at 11:19
  • mate, read the posts I linked. Those are SQL, Android's SQLite, Cursor and query examples. – Pier Giorgio Misley Dec 22 '16 at 11:20

1 Answers1

0

cursor query in DBManager.class

 public Cursor linkColumn(long _id) {
    String[] itemById = new String[]{DatabaseHelper.LINK};
    Cursor cursor = sqLiteDatabase
            .query(
                    DatabaseHelper.TABLE_NAME,
                    itemById,
                    DatabaseHelper.ID + "=" + _id,
                    null,
                    null,
                    null,
                    null);
    if (cursor != null) {
        cursor.moveToFirst();
    }
    return cursor;
}

clicking on item

 @Override
     public void onItemClick(long _id) {
     Intent returnIntent = new Intent();
     Cursor cursor = dbManager.linkColumn(_id);
     if(cursor.getCount()!=0){
     String item = cursor.getString(cursor.getColumnIndex(DatabaseHelper.LINK));
     returnIntent.putExtra("link", item);
     setResult(RESULT_OK, returnIntent);
     finish();}
     cursor.close();
  }
androiddev
  • 35
  • 7