如何从包含图像规格的drawable中检索图像并将其显示到ListView中

时间:2012-06-13 12:38:52

标签: android sqlite

我有drawable中的图像和数据库,其中包含诸如price和imageName之类的列名...所以我想检索所有imageNames列表并在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;
    }

1 个答案:

答案 0 :(得分:0)

如果您将drawable的名称存储在数据库中,则可以使用getIdentifier检索ID,然后您可以将ID传递给getDrawable,这将获得可用于ImageView的drawable。 / p>

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