我已经将数据存储在SQLite中。我需要从SQLite检索数据,单击按钮时它应该显示在文本视图上。当我再次单击按钮时,它将获得其他文本视图,并应在文本视图中显示。
使用此代码,我可以显示文本视图。当我单击按钮时,它显示了SQLite的所有内容,但一次只需要一个文本视图。
public void display(View v)
{
//use cursor to keep all data
//cursor can keep data of any data type
Cursor c=db.rawQuery("select * from mytable", null);
tv.setText("");
if(c!=null && c.getCount()>0)
{
//move cursor to first position
c.moveToFirst();
//fetch all data one by one
do
{
//we can use c.getString(0) here
//or we can get data using column index
String name=c.getString(c.getColumnIndex("name"));
String surname=c.getString(1);
//display on text view
tv.append(name+surname+"\n");
//move next position until end of the data
}while(c.moveToNext());
}
}
如下修改您的代码
Cursor c = null;
public void display(View v)
{
//use cursor to keep all data
//cursor can keep data of any data type
if(c == null) {
c=db.rawQuery("select * from mytable", null);
//move cursor to first position
c.moveToFirst();
}
tv.setText("");
if(c!=null && c.getCount()>0 && !c.isAfterLast())
{
//we can use c.getString(0) here
//or we can get data using column index
String name=c.getString(c.getColumnIndex("name"));
String surname=c.getString(1);
//display on text view
tv.setText(name+surname+"\n");
c.moveToNext();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句