This is my first try to work with databases in android.i want my app to get all the database data and present them in a list.My problem is that i m just presenting the last item of the database in my list.This is the code i m getting database data:
public String[] getData()
{
String[] columns =new String[]{DBHelper.ROWID, DBHelper.TITLE , DBHelper.AUTHOR, DBHelper.ISBN };
Cursor c=ourDatabase.query(DBHelper.DATABASE_TABLE, columns, null, null, null, null, null);
String sa = null;
String sb = null;
String sc = null;
int iRow=c.getColumnIndex(DBHelper.ROWID);
int is1=c.getColumnIndex(DBHelper.TITLE);
int is2=c.getColumnIndex(DBHelper.AUTHOR);
int is3=c.getColumnIndex(DBHelper.ISBN);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
sa=c.getString(is1);
sb=c.getString(is2);
sc=c.getString(is3);
}
return new String[] {sa,sb,sc};
}
What i have to return in order to see the whole database?Thanks
This is my onCreate:
HotOrNot entry2=new HotOrNot(this);
entry2.open();
String[] data2=entry2.getData();
entry2.close();
Toast.makeText(SQLView.this,data2[0].toString()+" "+data2[1].toString()+" "+data2[2].toString(), Toast.LENGTH_SHORT).show();
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
for(int i=0;i<data2.length;i+=3){
map = new HashMap<String, String>();
map.put("name",data2[i].toString());
map.put("address", data2[i+1].toString());
map.put("address2", data2[i+2].toString());
mylist.add(map);
}
// ...
ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
new String[] { "name", "address", "address2"},
new int[] {R.id.rtextView1,R.id.rtextView2,R.id.rtextView3});
lv.setAdapter(mSchedule);
@Aki
public String[] getData()
{
String[] columns =new String[]{DBHelper.ROWID, DBHelper.TITLE , DBHelper.AUTHOR, DBHelper.ISBN };
Cursor c=ourDatabase.query(DBHelper.DATABASE_TABLE, columns, null, null, null, null, null);
String result="";
String sa = null;
String sb = null;
String sc = null;
int iRow=c.getColumnIndex(DBHelper.ROWID);
int is1=c.getColumnIndex(DBHelper.TITLE);
int is2=c.getColumnIndex(DBHelper.AUTHOR);
int is3=c.getColumnIndex(DBHelper.ISBN);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
//result=result+c.getString(is1)+" "+c.getString(is2)+" "+c.getString(is3)+"\n";
sa=c.getString(is1);
sb=c.getString(is2);
sc=c.getString(is3);
}
return new String[] {sa,sb,sc};
}
and
public class SQLView extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
HotOrNot entry2=new HotOrNot(this);
entry2.open();
Cursor cursor = getContentResolver().query(DBHelper.DATABASE_TABLE, new String[] {DBHelper.TITLE, DBHelper.AUTHOR, DBHelper.ISBN}, null, null, null);
startManagingCursor(cursor);
// THE DESIRED COLUMNS TO BE BOUND
String[] columns = new String[] { DBHelper.TITLE, DBHelper.AUTHOR, DBHelper.ISBN };
// THE XML DEFINED VIEWS WHICH THE DATA WILL BE BOUND TO
int[] to = new int[] { R.id.rtextView1,R.id.rtextView2,R.id.rtextView3 };
// CREATE THE ADAPTER USING THE CURSOR POINTING TO THE DESIRED DATA AS WELL AS THE LAYOUT INFORMATION
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, columns, to);
// SET THIS ADAPTER AS YOUR LISTACTIVITY'S ADAPTER
this.setListAdapter(mAdapter);
}
}
Inside your
getDatamethod, you’re looping and on each iteration of the loop, you set sa, sb and sc. You don’t, however, add those to any collection until the loop exits so the array only holds the last set of values. If you still want to return a string array, you can do this:Then in your onCreate method you can simply do this: