java数据库只返回最后一个db项
这是我第一次尝试在安卓中使用数据库。我希望我的应用程序获取所有数据库数据并将它们显示在列表中。我的问题是,我只是在显示列表中数据库的最后一项。这是我获取数据库数据的代码:
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};
}
为了查看整个数据库,我必须返回什么?谢谢
这是我的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};
}
及
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);
}
}
# 1 楼答案
这是因为您正在覆盖字符串数组中的值,并且不可能在包含3个元素的字符串数组中返回整个数据库
更好的方法是返回游标并在需要时从游标检索数据。或者,您可以使用字符串数组的ArrayList来存储和返回方法中的值。请看JB Nizet的代码答案
编辑:查看onCreate,我建议直接使用游标创建游标适配器,并将其设置为ListAdapter。您不必经历地图的创建过程。这会让你的生活简单得多
下面是一个用于创建
SimpleCursorAdapter
的good tutorial# 2 楼答案
您的方法返回一个包含3个值的数组:sa、sb和sc(您应该使用更多描述性名称)。 您应该让它返回数组列表。列表中的每个数组都是表中的一行:
由于Java是一种OO语言,您甚至应该为每一行创建一个对象:
# 3 楼答案
在
getData
方法中,您正在循环,在循环的每次迭代中,您都会设置sa、sb和sc。但是,在循环退出之前,您不会将它们添加到任何集合中,因此数组只保存最后一组值。如果仍要返回字符串数组,可以执行以下操作:然后,在onCreate方法中,您可以简单地执行以下操作: