通过从android中的数据源检索图像名称,在列表视图中动态设置图像

时间:2014-08-18 17:31:09

标签: android image sqlite listview android-listview

drawable文件夹中检索图像的最佳方法是什么,而这些图片的名称存储在DB中,并在listView中显示?

假设我在drawable文件夹中有三张图片,其名称存储在DB中:

  • pic1.jpeg

  • image2.jpeg

  • another_image.jpeg

我还有一个名为getAllimages()的方法,我在其中检索图像名称并将其作为Cursor从DB返回:

public Cursor getAllImages(){
String sql = "SELECT iId as _id," + COLUMN_IMG_DESC + "," + COLUMN_IMG_NAME + " FROM " + TABLE_NAME";
    Cursor cursor = db.rawQuery(sql, null);
    if(cursor != null)
        cursor.moveToFirst();

    return cursor;
}

其中COLUMN_IMG_DESCimage descriptionCOLUMN_IMG_NAME是存储在数据库中的name of the image

然后我有CursorAdapter我尝试将图片及其描述映射到listView

    ListView customListView = (ListView)findViewById(R.id.lvCustom);    
    String[] from = { DatabaseHelper.COLUMN_IMG_DESC, DatabaseHelper.COLUMN_IMG_NAME};
            int[] to = {R.id.ivImg, R.id.tvTitle};
            SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.custom_listview, cursor, from, to, 0);

cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                ImageView imageImageView = (ImageView)findViewById(R.id.ivImg);

                String imageValue = cursor.getString(1); //get image names
                int[] imgResourceIds = new int[cursor.getColumnCount()]; //initialize array for resource Ids
                String images[] = new String[cursor.getColumnCount()];  //initialize an array for image names   

                for(int i=0; i<cursor.getColumnCount(); i++){
                    images[i] = imageValue; //store image names to an initialized array
                    imgResourceIds[i] = getResources().getIdentifier(images[i], "drawable", packageName); //get image name
                    imageImageView.setImageResource(imgResourceIds[i]); //set image to imageView
                }

                return true;
            }
        });

            customListView.setAdapter(cursorAdapter);

但我得到了这个结果:我怎么解决这个问题? enter image description here

提前致谢:)

3 个答案:

答案 0 :(得分:0)

您可以通过以下方式使用动态名称从drawable文件夹中获取图像:

resources.getIdentifier(String imageName, String folder, String package);

这将返回一个整数,方法与键入

的方式相同
R.drawable.imageName;

如何使用它的一个例子:

Bitmap bitmap[] = new Bitmap[5];

for (int i = 0; i < 5; i++)
    bitmap[i] = BitmapFactory.decodeResource(
        resources, 
        resources.getIdentifier("pic" +i, "drawable", "com.example.com")));

这可以从&#34; pic0.png&#34;,&#34; pic1.png&#34; ......直到&#34; pic4.png&#34;当然你可以按你想要的方式自定义

答案 1 :(得分:0)

int[] resID = new int[2]
String[] mDrawableName=new String[2];
mDrawableName[0] = "pic1";
mDrawableName[1] = "image";
mDrawableName[2] = "another_image";

for(i=0:i<=2:i++)
int resID[i] = getResources().getIdentifier(mDrawableName[i] , "drawable", getPackageName());

用法

image.setImageResource(resID[0]);

答案 2 :(得分:0)

好的问题是我分配了适配器布局参数错误。而不是 ListView_Row 我指的是 ListView 本身

所以:

 SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.custom_listview, cursor, from, to, 0);

将是:

SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.custom_listview_row, cursor, from, to, 0);