首页 > 解决方案 > 是否可以从同一个 SQLite 数据库中检索文本和图像

问题描述

就我而言,我有一个数据库,它有 4 列 1. id、2 Sticker_Name、3 Sticker_author 和第 4 列是 IMAGE,我知道如何从数据库中检索特定列,但我的问题是当我从 SQLite 检索特定列时图像也在 recyclerListView 中检索和显示,我不明白我能做些什么来解决这些问题。

这是我的代码..

要从数据库中检索特定列,它工作得很好,但是当我保存在数据库中的第二个活动中时,图像也会显示在 recyclerListView 中。

 private void fetchdat(){
    word_list = new ArrayList<>();
    SQLiteDatabase sd=mydb.getWritableDatabase();
    Cursor cursor1=sd.query("stickerstable",new String[] {STICKER_AUTHOR},null,null,null,null,null);
   if (cursor1.moveToFirst()){
       do {

           word_list.add(new data_items(
                   cursor1.getString(cursor1.getColumnIndex(STICKER_AUTHOR))
           ));
       }while (cursor1.moveToNext());
       cursor1.close();
       sd.close();
   }

    mAdapter = new recycle_Adapter(word_list);
    recyclerView.setAdapter(mAdapter);
}

这是代码

因此,我从 SQLite 数据库中检索所有列数据。

  private void FetchData(){
    word_list = new ArrayList<>();
     SQLiteDatabase sd = mydb.getWritableDatabase();
    Cursor cursor = sd.query("stickerstable",null, null, null, null, null, null);
    if (cursor.moveToFirst()){
        do{
            word_list.add(new data_items(
                    cursor.getInt(cursor.getColumnIndex(ID)),
                    cursor.getString(cursor.getColumnIndex(STICKER_AUTHOR)),
                    cursor.getString(cursor.getColumnIndex(STICKER_NAME)
                     )));

        }

        while (cursor.moveToNext());
        cursor.close();
        sd.close();
    }


     mAdapter = new recycle_Adapter(word_list);
    recyclerView.setAdapter(mAdapter);


  }

标签: javaandroiddatabasesqliteandroid-sqlite

解决方案


也许这可以帮助你:

private List<String> getAuthorList () {
            List<String> authorList = new ArrayList<>();
            try {
                SQLiteDatabase sd = mydb.getWritableDatabase();
                Cursor cursor = sd.rawQuery(
                        "SELECT Sticker_author FROM " + tableName + " WHERE <here is your condition> ", null);

                if (cursor.getCount() > 0) {
                    cursor.moveToFirst();

                    while (!cursor.isAfterLast()) {
                        String author    = cursor.getString(0);
                        authorList.add(author);
                        cursor.moveToNext();
                    }

                    if (cursor != null) {
                        cursor.close();
                    }

                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            return authorList;
        }

推荐阅读