【问题标题】:Set image dynamically in a listview by retrieving image name from a data source in android通过从android中的数据源检索图像名称在列表视图中动态设置图像
【发布时间】:2014-10-11 16:40:50
【问题描述】:

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

假设我在drawable 文件夹中有三张图片,它们的名称存储在数据库中:

  • pic1.jpeg

  • image2.jpeg

  • another_image.jpeg

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

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 是存储在DB 中的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);

但我得到了这个结果:我如何解决这个问题?

提前致谢:)

【问题讨论】:

    标签: android image sqlite listview android-listview


    【解决方案1】:

    您可以通过这种方式从可绘制文件夹中获取具有动态名称的图像:

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

    这会从“pic0.png”、“pic1.png”...直到“pic4.png”获取图像,当然你可以按照你想要的方式自定义它

    【讨论】:

    • 感谢@TomTsagk 的帮助,但是如果图像的名称彼此不同,就像我现在编辑的示例一样:1.pic.jpeg、2.image.jpeg、3.another_image.jpeg ...这是否意味着我必须重命名数据库中的所有图像?
    • @hrskrs 如果你没有注意到我使用了 "pic" +i 这意味着如果你在 for 循环中输入它,你可以获得 "pic0.png"、"pic1.png"... “picX.png”并将它们存储在一个数组或任何东西中(在处理可绘制文件夹中的图像时,“.png”部分被忽略):)
    • 是的,我注意到了,但我要问的问题是,如果图片名称彼此不同怎么办?不像 pic1.jpeg、pic2.jpeg 而是像 pic1.jpeg、image_2.jpeg、another_image.jpeg?
    • @hrskrs 您可以将名称保存在字符串数组和 imageName 部分类型 stringArray[i] 或类似的东西上,我不确定您是如何保存名称的
    【解决方案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());
    

    用法

    image.setImageResource(resID[0]);
    

    【讨论】:

    • @TomTsagk 我这样尝试过(编辑了问题)-> 更新了 cursorAdapter 并添加了打印屏幕的图像。这就是我得到的结果。我的错在哪里?
    【解决方案3】:

    好的,问题是我分配了我的适配器布局参数错误。而不是 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多