我有一个sqlite数据库,我希望从中显示组中的数据。但是sqlite只显示最后一列的数据。我的代码如下:
String q="Select * from events";
Cursor m = eventsDB.rawQuery(q, null);
if(m.moveToFirst() )
while(m.moveToNext()){
{
String curevent = m.getString(m.getColumnIndex("name"));
String curdate = m.getString(m.getColumnIndex("date"));
evname.setText(curevent);
}
}
else {
evname.setText("Not found");
}
如何显示所有数据?
答案 0 :(得分:0)
这就是你告诉Android要做的事情。您循环遍历Cursor
上的所有行,同时设置文本;最后一个是它将显示的那个,因为它是Cursor
答案 1 :(得分:0)
如何显示所有数据?
使用此方法从表格中获取所有数据
public String getAllData() {
String[] columns = new String[] { name, date };
Cursor c = ourDatabase.query(table_name, columns, null, null, null,
null, null);
String someresult = "";
int iName = c.getColumnIndex(name);
int iDate = c.getColumnIndex(date);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
someresult += c.getString(iName ) + "\n" + c.getString(iDate )
+ "\n";
}
return someresult;
}
其中名称和日期是您的列名,ourDatabase
是SQLiteDatabase
类的实例。
如果你有更多列,那么在String数组中添加列名,然后获取每个索引并通过getString()获取所有数据。
答案 2 :(得分:0)
如果要将所有名称设置为单个文本视图,请执行此操作。
String q="Select * from events";
Cursor m = eventsDB.rawQuery(q, null);
String allnames="";
String abc[]={"","","","","","","","","","","","","","",""};
int q=0
if(m.moveToFirst() )
while(m.moveToNext()){
{
String curevent = m.getString(m.getColumnIndex("name"));
String curdate = m.getString(m.getColumnIndex("date"));
abc[q]=curevent;
allnames=allnames+abc[q];
evname.setText(allnames);
q++
}
}
else {
evname.setText("Not found");
}