0

我有可绘制的图像和一个具有列名(例如价格和图像名称)的数据库...所以我想检索所有图像名称列表并在 listView 上显示相应的图像以及数据库中的其他字段...所以这里是什么我已经试过了。。

//getting a list of names from database

public List<String> populateList(){
        List<String> ItemsList = new ArrayList<String>();
        DatabaseHelper openHelper = new DatabaseHelper(this);
        SQLiteDatabase sqliteDatabase = openHelper.getReadableDatabase();

        // We need a a guy to read the database query. Cursor interface will do it for us
                //(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
                Cursor cursor = sqliteDatabase.query(DBAdapter.ITEMS_TABLE, null, null, null, null, null, null);
                // Above given query, read all the columns and fields of the table

                startManagingCursor(cursor);

                while (cursor.moveToNext()) {
                    // In one loop, cursor read one undergraduate all details
                    // Assume, we also need to see all the details of each and every undergraduate
                    // What we have to do is in each loop, read all the values, pass them to the POJO class
                    //and create a ArrayList of undergraduates

                    String BName = cursor.getString(cursor.getColumnIndex(DBAdapter.COLUMN_BOOK_NAME));
                    String Bauthor = cursor.getString(cursor.getColumnIndex(DBAdapter.COLUMN_BOOK_AUTHOR));
                    String Bgenre = cursor.getString(cursor.getColumnIndex(DBAdapter.COLUMN_BOOK_GENRE));
                    String Bpublisher = cursor.getString(cursor.getColumnIndex(DBAdapter.COLUMN_BOOK_PUBLISHER));
                    String Bcover = cursor.getString(cursor.getColumnIndex(DBAdapter.COLUMN_BOOK_COVER_TYPE));
                    int Bpages = cursor.getInt(cursor.getColumnIndex(DBAdapter.COLUMN_BOOK_NUM_PAGES));
                    int Bage = cursor.getInt(cursor.getColumnIndex(DBAdapter.COLUMN_BOOK_AGE_RESTRICTION));
                    int Bqty = cursor.getInt(cursor.getColumnIndex(DBAdapter.COLUMN_BOOK_QUANTITY));
                    double Bprice = cursor.getDouble(cursor.getColumnIndex(DBAdapter.COLUMN_BOOK_PRICE));
                    String Bimage = cursor.getString(cursor.getColumnIndex(DBAdapter.COLUMN_BOOK_IMAGE));
                    //String uHaddress = cursor.getString(cursor.getColumnIndex(DBAdapter.COLUMN_ADDRESS));
                    //double ugGpa = cursor.getDouble(cursor.getColumnIndex(AndroidOpenDbHelper.COLLUMN_NAME_UNDERGRADUATE_GPA));

                    // Finish reading one raw, now we have to pass them to the POJO
                    BooksPojo uPojoClass = new BooksPojo(BName, Bauthor, Bgenre, Bpublisher, Bcover, Bpages, Bage, Bqty, Bprice, Bimage);


                    // Lets pass that POJO to our ArrayList which contains undergraduates as type
                    pojoArrayList.add(uPojoClass);

                    // But we need a List of String to display in the ListView also.
                    //That is why we create "uGraduateNamesList"
                    ItemsList.add(Bimage);
                }

                // If you don't close the database, you will get an error
                sqliteDatabase.close();
    return ItemsList;
    }
4

1 回答 1

0

如果您将drawable的名称存储在数据库中,则可以使用getIdentifier检索ID ,然后将ID传递给getDrawable,这将使drawable与ImageView一起使用。

要将所有这些数据放入您的 ListView 中,您应该使用ListAdapter。SimpleCursorAdapter可能最适合您的需求。

于 2012-06-13T12:52:49.263 回答