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?