1

I am implementing SQLite in android to get all the rows data using 2-d strings. But the strings are giving me a null value except for the value of the last index

Code I wrote to get all the data of table

public String[][] getAllData(){
    MyDBHelper myDBHelper=new MyDBHelper(context);
    SQLiteDatabase db=myDBHelper.getWritableDatabase();
    String columns[]={MyDBHelper.UID, MyDBHelper.USERNAME, MyDBHelper.MOBILE,MyDBHelper.EMAIL};
    Cursor cursor=db.query(MyDBHelper.TABLE_NAME,columns,null,null,null,null,null);

    int index_username=cursor.getColumnIndex(MyDBHelper.USERNAME);
    int index_mobile=cursor.getColumnIndex(MyDBHelper.MOBILE);
    int index_email=cursor.getColumnIndex(MyDBHelper.EMAIL);
    Log.i("tag",Integer.toString(cursor.getCount()));
    String strData[][]=new String[cursor.getCount()][3];
    while(cursor.moveToNext()){

        int index=0;
        Log.i("tag",cursor.getString(index_username));
        strData[index][0]=cursor.getString(index_username);
        strData[index][1]=cursor.getString(index_mobile);
        strData[index][2]=cursor.getString(index_email);

        Log.i("tag",strData[index][0]+"data");
        index++;
    }
    return strData;
}

the code which called the method

public void getData(){
    String data[][]=myDBAdaptor.getAllData();

    Log.i("tag", Integer.toString(data.length));
    for(int i=0;i<data.length;i++) {
        //Log.i("tag",data[i][0]);
        String username=data[i][0];
        String mobile=data[i][1];
        String email=data[i][2];
        DataProvider dataProvider= new DataProvider(username,mobile,email);
            listDataAdaptor.add(dataProvider);

    }
}

the problem Suppose the strData={{"abc","123","xyz"},{"efg","456","pqr"},{"mno","789","fgh"}};

after the execution of this statement data[][]=mydBAdaptor(); the value of data is printing data[][]={{null,null,null},{null,null,null},{"mno","789","fgh"}} why the value are getting null? please help i am trying for hours but could resolve this bug.

Rishabh Jindal
  • 75
  • 1
  • 10
  • I think you should use a list or an array of objects by creating a class for the element you're trying to get from your Database just to make things simpler. Using a multidimensional array for such a task is way too hard to mantain... – LuigiPower Jul 22 '16 at 17:07
  • 1
    By "2D Strings"... you do mean bidimensional arrays, do you? – Phantômaxx Jul 22 '16 at 17:09
  • A question, what is the line "Log.i("tag",strData[index][0]+"data");" printing in the logs? is it showing the correct data? – LuigiPower Jul 22 '16 at 17:11
  • 1
    @LuigiPower i just followed what you said and it worked like a magic:)... but still curious why it was not working for the string. yes it is printing data in getAllData() – Rishabh Jindal Jul 22 '16 at 17:20
  • Happy to be of help. I guess the problem was something regarding references, someone will be able to notice it and post an answer sooner or later. – LuigiPower Jul 22 '16 at 17:21
  • @Rotwang there was no tag for 2-d strings – Rishabh Jindal Jul 22 '16 at 17:22
  • Because it's not a word. – Phantômaxx Jul 22 '16 at 17:41

0 Answers0