amardco amardco - 1 month ago 21
Android Question

get array of database info android?

this is my code for getting data from database,but any things not show.what's wrong about it

public String[] GetData() {

String[] result =new String[]{};
int counter=0;
String[] Columns = new String[]{KEY_ID, KEY_NAME, KEY_PHONE, KEY_EMAIL, KEY_ADDRESS};

Cursor cr = myDB.query(DATABASE_TABLE, Columns, null, null, null, null, null);

int ROW_NAME = cr.getColumnIndex(KEY_NAME);
int ROW_PHONE = cr.getColumnIndex(KEY_PHONE);
int ROW_EMAIL = cr.getColumnIndex(KEY_EMAIL);
int ROW_ADDRESS = cr.getColumnIndex(KEY_ADDRESS);


for (cr.moveToFirst(); !cr.isAfterLast(); cr.moveToNext()) {
result[counter]= cr.getString(ROW_NAME) + " " + cr.getString(ROW_PHONE) + " " + cr.getString(ROW_EMAIL) + " " + cr.getString(ROW_ADDRESS)+"\n";
counter++;
}
return result;
}

Answer

Initialize result this way:

String[] result =new String[cr.getCount()];