Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在片段中显示sqlite数据。。!_Android_Android Fragments_Android Sqlite - Fatal编程技术网

Android 如何在片段中显示sqlite数据。。!

Android 如何在片段中显示sqlite数据。。!,android,android-fragments,android-sqlite,Android,Android Fragments,Android Sqlite,我有活动,我可以把2个文本在其中,并在数据库中保存 我有3个标签,每个标签上有不同的图片 请有人可以帮助我的代码,我可以显示在每个选项卡上的这2个文本!我不知道我怎么能做到 SQLITE活动: 表1: } 只需使用下面的代码从表中检索数据 class DbResponse{ public String name; public String place; } 现在 在fragment类中编写 DataBaseHelper db = new DataBaseHelper(getActivit

我有活动,我可以把2个文本在其中,并在数据库中保存 我有3个标签,每个标签上有不同的图片 请有人可以帮助我的代码,我可以显示在每个选项卡上的这2个文本!我不知道我怎么能做到

SQLITE活动:

表1:

}


只需使用下面的代码从表中检索数据

class DbResponse{
public String name;
public String place;
}
现在

在fragment类中编写

DataBaseHelper db = new DataBaseHelper(getActivity());
DbResponse response = db.getData();

textview.setText(response.name+" - "+response.place);

希望它能帮助你

谢谢你的快速回答,但我不知道如何在片段中编写代码:(我有textView,不知道如何从sqlhey中提取。我已经更改了我的答案。只需在片段中创建DataBaseHelper类的一个对象,并使用该对象调用其检索函数(您必须在此类中创建一个)并在该函数中编写我的上述代码;在游标之前?因为在db中保存新名称和位置时,它们存储在下一行位置的表中。因此,如果只想检索最新值,只需在将它们保存到数据库表之前使用dropTable query删除表即可。
class DbResponse{
public String name;
public String place;
}
public DbResponse getData(){
  DbResponse obj = new DbResponse();
  SQLiteDatabase db = this.getWritableDatabase();
  Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null);
  if (cursor.getCount() > 0) {
        cursor.moveToFirst();
  obj.name = cursor.getString(cursor.getColumnIndex(NAME));
  obj.place = cursor.getString(cursor.getColumnIndex(PLACE));
  }
  cursor.close();
  return obj;
}
DataBaseHelper db = new DataBaseHelper(getActivity());
DbResponse response = db.getData();

textview.setText(response.name+" - "+response.place);