-1

I want to fetch data from multiple rows and columns using curser using this below code

  Cursor getallrecords(){

    SQLiteDatabase dbb = this.getReadableDatabase();

    Cursor cursor = null;

    if (dbb != null){
        Log.d("cursor","not null");
      
        cursor = dbb.rawQuery(" SELECT * FROM " + TABLE_NAME,null);

        }else{
        Log.d("cursor","null");
    }

    return cursor;
    }

On logcat it shows the cursor is not empty and returns data. But when I try to set that data which cursor carries on ArrayList using the below code.

  void read(){
    Cursor cursor = save_barcode.getallrecords();

    if (cursor.getCount() != 0){
        cursor.moveToFirst();
        while(cursor.moveToNext()){
            url.add(cursor.getString(1));
            type.add(cursor.getString(2));
            date_and_time.add(cursor.getString(3));
        }
    }

    if (!url.isEmpty()){
        Log.d("message","not null");
    }else {
        Log.d("url","null");
    }

    if (!type.isEmpty()){
        Log.d("message","not null");
    }else {
        Log.d("type","null");
    }
    if (!date_and_time.isEmpty()){
        Log.d("message","not null");
    }else {
        Log.d("date","null");
    }
}

Then in logcat it shows URL,date_and_time, type all of these three are empty. So if anyone knows how to fetch data from multiple rows and column using cursor then help me

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Akshit
  • 7
  • 1
  • 2
  • Does this answer your question? [What's the best way to iterate an Android Cursor?](https://stackoverflow.com/questions/10723770/whats-the-best-way-to-iterate-an-android-cursor) – javdromero Jul 15 '21 at 16:34

1 Answers1

1

The obvious problem with your code is that you are using moveToFirst() and after that moveToNext(), so it is certain that you lose the 1st row.
If all you have in the table is just 1 row then the lists will be empty.
No need to check getCount(), so remove if and no need for moveToFirst().

Change to this:

void read(){
    Cursor cursor = save_barcode.getallrecords();
    if (cursor == null) return; 
    while (cursor.moveToNext()) {
        url.add(cursor.getString(1));
        type.add(cursor.getString(2));
        date_and_time.add(cursor.getString(3));
    }
    cursor.close();

   ...................
} 
forpas
  • 160,666
  • 10
  • 38
  • 76
  • Bro my apk still crash when i try to set data on recycle view. In logcat it shows java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference. – Akshit Jul 16 '21 at 06:26
  • @Akshit this is not because of the code in my answer. Your app crashes when you call `setText()`. – forpas Jul 16 '21 at 06:40
  • Because settext method does not get any value this means obviously there is something we r missing in fetching data. – Akshit Jul 16 '21 at 07:16
  • @Akshit no this is not the problem. The problem is that the TextView on which you call setText is null. This is what *null object reference* means. – forpas Jul 16 '21 at 07:18
  • Look bro i use holder.getBarcodeUrl.setText(String.valueOf(url.get(position))); on my recycleview holder and the url is refering to a arraylist which geting from read method by using cunstructor. – Akshit Jul 16 '21 at 07:22
  • @Akshit this is another problem for which you must ask a new question where you explain what the problem is. Questions are not asked in the comments section. – forpas Jul 16 '21 at 07:23
  • Thanks, bro my problem is solved. The fault was in the adapter class. – Akshit Jul 16 '21 at 08:19
  • @Akshit fine if it is solved and don't forget to accept the answer by clicking on the checkmark. – forpas Jul 16 '21 at 08:20