0

I'm trying to return all data from my two column DB into two different TextViews. In one attempt, I can return all the data, but it's only in one TextView, in the second attempt, I can get it into the two TextViews, but it only returns the first row from the DB. First try:

    //--- Returns only first result

//--- DB Method
public String[] getPres2() {
    String[] columns = new String[] { KEY_PRES, KEY_YEAR};
    Cursor cursor = db.query(DB_TABLE, columns, null, null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
        String pstYr[] = new String[2];
        pstYr[0] = cursor.getString(0);
        pstYr[1] = cursor.getString(1);
        cursor.close();
        this.db.close();
        return pstYr;
    }
    return null;

}

//--- in the activity
public void DBMethod2() {
    dba = new DBAdapter(this);
    dba.open();
    String[] pst_Str = dba.getPres2();
    dba.close();
    pst_TV.setText(pst_Str[0]);
    yrs_TV.setText(pst_Str[1]);

}

and the second try:

    //--- Returns all results, but only in a single TextView
//--- DB Method
public String getPres3() {
    String[] columns = new String[] { KEY_PRES, KEY_YEAR};
    Cursor cursor = db.query(DB_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iPrs = cursor.getColumnIndex(KEY_PRES);
    int iYr = cursor.getColumnIndex(KEY_YEAR);

    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
        result = result + cursor.getString(iPrs) + "\n" + cursor.getString(iYr) +   "\n" + "\n";
    }

    return result;

}


//--- In Activity
public void DBMethod3() {
    dba = new DBAdapter(this);
    dba.open();
    String pstY_Str = dba.getPres3();
    dba.close();
    pst_TV.setText(pstY_Str);

}

Any help?

kirktoon1882
  • 1,221
  • 5
  • 24
  • 40

2 Answers2

0

Your first attempt will work if you loop through all of the rows in the cursor.

Here is an example from this link

 String arr[]=new String[4];
 i=0;

 cur.moveToFirst();
 while (cur.isAfterLast() == false) 
 {
     arr[i]  = cur.getString(0);
     i++;
     cur.moveToNext();
 }
Community
  • 1
  • 1
Glenn
  • 175
  • 2
  • 15
  • I can't get your solution to work. First, it only returns the first DB row, and second, only the first TextView has a string in it, the second TextView is blank. – kirktoon1882 Sep 30 '13 at 21:21
0

You have to iterate through the cursor, one row at a time, and then use the GetString syntax like this:

String mId = cursor.getString(cursor.getColumnIndex(COLUMN_ID));

where COLUMN_ID is the name of the column in the sqlite database

Glenn
  • 175
  • 2
  • 15
  • I think what you're saying is this: `public String[] getPres() { String[] columns = new String[] { KEY_PRES, KEY_YEAR }; Cursor cursor = db.query(DB_TABLE, columns, null, null, null, null, null); String result[] = new String[2]; for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { result[0] = cursor.getString(cursor.getColumnIndex(KEY_PRES)); result[1] = cursor.getString(cursor.getColumnIndex(KEY_YEAR)); } return null; }` – kirktoon1882 Oct 01 '13 at 19:44
  • ...which I can't seem to get to work either. It's okay, though, because I've solved the problem another way. Rather than use a DB, I'm now creating a table and dynamically adding the rows which have the two columns in them. – kirktoon1882 Oct 01 '13 at 19:46