在动态列表视图通过检索图像名来自Android的数据源设置图像

在动态列表视图通过检索图像名来自Android的数据源设置图像

本文介绍了在动态列表视图通过检索图像名来自Android的数据源设置图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是检索绘制图像的最佳方式文件夹中,而这些图片的名称都存储在 DB ,并显示在一个的ListView

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

  • pic1.jpeg

image2.jpeg

image2.jpeg

我也有一个名为方法 getAllimages()其中i检索图像名称并返回它们作为一个光标从DB:

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;
}

其中, COLUMN_IMG_DESC 图片说明 COLUMN_IMG_NAME 是存储在数据库中的图像的名称

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

然后我有一个的CursorAdapter ,其中我试图地图图片及其说明到的ListView

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?

在此先感谢:)

推荐答案

好吧,问题是,我被分配我的适配器布局参数的错误。取而代之的 ListView_Row 我指着的ListView 本身

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

所以:

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

这篇关于在动态列表视图通过检索图像名来自Android的数据源设置图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 22:59