Set image dynamically in a listview by retrieving image name from a data source in android

喜你入骨 提交于 2019-12-25 02:37:31

问题


What is the best way to retrieve images from drawable folder while the name of those images are stored in a DB and show them in a listView ?

Suppose i have three pictures in drawable folder and their names are stored in DB as:

  • pic1.jpeg

  • image2.jpeg

  • another_image.jpeg

I also have a method called getAllimages() where i retrieve image names and return them as a Cursor from 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;
}

Where COLUMN_IMG_DESC is an image description and COLUMN_IMG_NAME is the name of the image stored in DB

Then i have a CursorAdapter where i tried to map images and their descriptions to a 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);

but i got this result: How tan i solve this?

Thanks in advance :)


回答1:


You can get an image from the drawable folder with a dynamic name this way:

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

This returns an integer the same way you would type

R.drawable.imageName;

An example of how to use it:

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")));

This gets images from "pic0.png", "pic1.png" ... until "pic4.png", of course you can customize it the way you want




回答2:


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());

Usage

image.setImageResource(resID[0]);



回答3:


Ok the problem was that i was assigning my Adapter Layout Parameter wrong. Instead of ListView_Row i was pointing to ListView itself

So:

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

Would be:

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


来源:https://stackoverflow.com/questions/25368871/set-image-dynamically-in-a-listview-by-retrieving-image-name-from-a-data-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!