Hello i am new in android and i really need help with something. I got a prepopulate database in my sqlitehelper class and i need a query request returns a String[] table.
Example my database got 3 columns id, name, tel my query will be based on id so the cursor will get {1 mary 2134} i want that to be converted to an String [1 mary 2134]. Dont know if it helps but all my requests will be getting a single row returned, cause the query its primary key based.
my code is something like that:
public String[] getSingleRow(String id){
String[] asColumnsToReturn = new String[] { COLUMN_ID,COLUMN_NAME, COLUMN_TELEPHONE}
Cursor cursor = this.dbSqlite.query(TABLE_NAME, asColumnsToReturn, COLUMN_ID + "=" + id, null, null, null, null);
//here is where i need help
String table[]=new String[2];
int i = 0;
while (i < table.length) {
table[i] = cursor.getString(i);
i++;
cursor.moveToNext();
}
cursor.close();
return table;
}
I need that so in my main class i can set textview like:
TextView myTextView = (TextView)findViewById(R.id.textView1);
Table= SQLiteHelper.getSingleRow(id);
myTextView.setText(Table[0]); // "0"for id "1" for name and "2" for telephone
So my question is how do i convert a single row returned query to a String[]
You seem to be confused on what a cursor is.
A cursor points to one row in your results response. So if your table looks like this:
The cursor can only point to one of these rows at a time until you call cursor.movetoNext(). In a single row result, your cursor obviously will never have to use that method.
Of course, if you want a single ROW and not a TABLE like your variable is named:
Also, I would suggest using constants for the columns as well: