【问题标题】:How to display sqlite data in fragments..!如何在片段中显示 sqlite 数据..!
【发布时间】:2016-09-09 05:30:22
【问题描述】:

我有一个活动,我可以在其中放入 2 个文本并保存在数据库中 我有 3 个标签,每个标签都有不同的图片 请有人可以帮助我提供可以在每个选项卡上显示这两个文本的代码!不知道怎么弄!

SQLITE 活动:

表 1:

public class DataBaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "big.db";
public static final String TABLE_NAME = "images";
public static final String NAME = "name";
public static final String PLACE = "place";
public static final String KEY_ID = "id";


public DataBaseHelper(Context context) {

    super(context, DATABASE_NAME, null, 1);
    SQLiteDatabase db = this.getWritableDatabase();

}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_IMAGES_TABLE = "CREATE TABLE images ( " +
            "id" + "INTEGER PRIMARY KEY AUTOINCREMENT, " +

            "name TEXT, " +
            "place TEXT )";

    db.execSQL(CREATE_IMAGES_TABLE);
}


@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    db.execSQL("DROP TABLE IF EXISTS images");
    this.onCreate(db);
}

public void insertentry(String name, String place) {

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(NAME, name);
    contentValues.put(PLACE, place);

    db.insert(TABLE_NAME, null, contentValues);
}

}

public class Tab1 extends Fragment {
    DataBaseHelper db;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.tab1, container, false);

    ImageView imageView = (ImageView) v.findViewById(R.id.img1);
    String photoPath = Environment.getExternalStorageDirectory() + "/Download/image1.jpg";
    Bitmap bitmap1 = BitmapFactory.decodeFile(photoPath);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;
    final Bitmap b = BitmapFactory.decodeFile(photoPath, options);

    imageView.setImageBitmap(b);


    return v;

}

【问题讨论】:

    标签: android android-fragments android-sqlite


    【解决方案1】:

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

    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);
    

    希望对你有帮助

    【讨论】:

    • 感谢您的快速回答,但我不知道如何在片段中编写代码 :( 我在那里有 textView 并且不知道如何从 sql 中提取
    • 嘿,我改了答案。
    • 只需在片段中创建 DataBaseHelper 类的对象,并使用该对象调用其检索函数(您必须在此类中创建一个),然后在该函数中编写我的上面代码。
    • 我应该把 SQLiteDatabase db = this.getreadableDatabase();在光标之前?
    • 因为当您在数据库中保存新的 nameplace 时,它们将存储在下一行位置的表中。因此,如果您只想检索最新值,只需使用 dropTable 查询删除表,然后再将它们保存到数据库表中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 2018-04-24
    • 1970-01-01
    • 2018-11-21
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多