Sqlite listview仅显示最后一个列或一个

时间:2017-11-11 12:28:53

标签: android database sqlite listview android-sqlite

嗨我使用sqlite我需要显示所有的coulms名称,但listview只显示最后的colums名称检查图像enter image description here   One is last added

  private void ListShow() {
            list =  findViewById(R.id.list);
            myDb.getReadableDatabase();
            myDb.getWritableDatabase();
            Cursor res = myDb.getAllData();
            while (res.moveToNext()) {
                String[] strNAME = new String[]{res.getString(0)
                };
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, android.R.id.text1, strNAME);
                list.setAdapter(adapter);
    adapter.notifyDataSetChanged();
            }
        }
public Cursor getAllData() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
        return res;
    }

1 个答案:

答案 0 :(得分:0)

你的函数需要返回object的ArrayList,参见例子:

public ArrayList<FavoriteObj> selectAllRow() {
            String query = "SELECT * FROM " + tableName;

            ArrayList<FavoriteObj> favoriteObjs= new ArrayList<>();
            try {
                Cursor cursor = this.getReadableDatabase().rawQuery(query, null);
                if (cursor.moveToFirst()) {
                    do {
                        favoriteObjs.add(new FavoriteObj(
                                String.valueOf(cursor.getString(1))
                                , cursor.getString(2)
                                , null
                                , cursor.getInt(3)));
                    } while (cursor.moveToNext());
                }
                cursor.close();
            } catch (Exception e) {
                Log.i("error", e.toString());
            }
            this.getReadableDatabase().close();
            return favoriteObjs;
}