2012-06-13 28 views
0

我有可绘制的图像和具有列名称的数据库,如price和imageName ...所以我想检索所有imageNames列表并显示与数据库中的其他领域......所以在这里我已经尝试了现在一个ListView对应的图像一起..如何从包括图像规格的可绘制图像检索图像并将其显示到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; 
    } 

回答

0

如果您存储在数据库中你绘制的名字,你可以使用然后检索ID getIdentifier然后,您可以将ID传递给getDrawable,这将得到可用于您的ImageView的drawable。

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

+0

你能给我举个例子怎么做 – user1430923

相关问题