Android 如何从SQLite检索数据并在单击按钮时在TextView中一次只显示一个数据
我已经在SQLite中存储了数据。我需要从SQLite检索数据,当单击按钮时,它应该显示在文本视图上。当我再次点击按钮时,它会得到其他的文本视图,并且会显示在文本视图中 使用此代码,我可以显示文本视图。当我点击按钮时,它会显示SQLite中的所有内容,但我一次只需要一个文本视图Android 如何从SQLite检索数据并在单击按钮时在TextView中一次只显示一个数据,android,xml,sqlite,Android,Xml,Sqlite,我已经在SQLite中存储了数据。我需要从SQLite检索数据,当单击按钮时,它应该显示在文本视图上。当我再次点击按钮时,它会得到其他的文本视图,并且会显示在文本视图中 使用此代码,我可以显示文本视图。当我点击按钮时,它会显示SQLite中的所有内容,但我一次只需要一个文本视图 public void display(View v) { //use cursor to keep all data //cursor can keep data of any data type
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();
}
}
只有一个文本视图意味着什么。或者你想在
列表视图中显示,或者?我得到了答案。谢谢你的帮助。我需要一个代码。当我按下其他按钮时,它应该返回到上一个文本视图。请帮助我。光标cb=null;public void back(View v){//use cursor to keep all data//cursor可以保留任何数据类型的数据,如果(cb==null){cb=db.rawQuery(“从mytable中选择*,null);//将光标移动到第一个位置cb.moveToPrevious();}tv.setText(“”);如果(cb!=null&&cb.getCount()>0&&c.moveToPrevious()){//我们可以在这里使用c.getString(0)//或者我们可以使用列索引字符串name=cb.getString(c.getColumnIndex(“name”);字符串姓氏=cb.getString(1);//显示在文本视图tv.setText(name+姓氏+“\n”);编写此代码时出错。当我按下按钮时,它不会移动到上一个文本视图。