【问题标题】:Load Image in a custom list by SimpleCursorAdapter通过 SimpleCursorAdapter 在自定义列表中加载图像
【发布时间】:2013-04-01 16:36:34
【问题描述】:

我有一个自定义列表,我正在通过 SimpleCursorAdapter 在其中加载数据..我的列表有一个图像视图,我想为列表的每一行更改其图像..我使用了 viewbilnder..但没有得到任何东西.. 这是我的适配器代码

private void getData() {


        try {

        // obtain a list of from DB

            db = SQLiteDatabase.openDatabase(ClubCP.DbPath,null,SQLiteDatabase.CREATE_IF_NECESSARY);
            String TABLE_NAME = "cat";
            String COLUMN_ID = "_id";
            String COLUMN_POET_ID = "poet_id";
            String COLUMN_TEXT = "text";
            String COLUMN_PARENT_ID = "parent_id";
            String [] columns ={COLUMN_ID,COLUMN_TEXT,COLUMN_POET_ID};
            String mySQL = "SELECT DISTINCT id as _id "+","+COLUMN_POET_ID+","+COLUMN_TEXT+","+COLUMN_PARENT_ID
                    + " from "+TABLE_NAME
                    + " where " + COLUMN_PARENT_ID+"= 0 "
                    + " order by " + COLUMN_POET_ID+"= 0 ";
          // String mySQL="SELECT DISTINCT id as _id,poet_id,text,parent_id from cat where parent_id=0 order by poet_id";              
        //Cursor c = db.query(TABLE_NAME,columns,COLUMN_PARENT_ID+"= 0", null, null, null, COLUMN_POET_ID);
         Cursor c = db.rawQuery(mySQL, null);

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, c, 
                columns, new int[] {R.id.list_item_text_sub,R.id.list_item_text_main,R.id.list_item_text_id}, 0);
    adapter.setViewBinder(new CustomViewBinder());  
    ListView list = (ListView) findViewById(R.id.list_poet_name);
    list.setAdapter(adapter);



        } catch (Exception e) {
        Toast.makeText(this, e.getMessage(), 1).show();
        }


}

这是viewbinder

    public class CustomViewBinder implements ViewBinder {

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

        if (columnIndex == cursor.getColumnIndex(ClubCP.KEY_POET_ID)) {
            // If the column is IS_STAR then we use custom view.
            int poet_id = cursor.getInt(columnIndex);

            String path = ClubCP.SDcardPath + "/temp/"+poet_id+".jpg"; 
            File imgFile = new File(path);
            ImageView img=(ImageView)findViewById(R.id.list_item_img);
            if(imgFile.exists())
        {
                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());                  

                img.setImageBitmap(myBitmap);
        }
            else                    
               Toast.makeText(Read.this,"no IMAGE IS PRESENT'",Toast.LENGTH_SHORT).show();


            img.setImageResource(R.drawable.music_album_header_vinyl);
        }
        /// if (poet_id != 6) {

                 //TextView verse = (TextView)findViewById(R.id.poem_verse_list_item_01);
                // verse.setGravity(Gravity.LEFT);

        //  } else {

        //  }
        //  return true;
    //  }

        return false;
    }

}

有人可以帮我解决这个问题吗? 我搜索并找到了一种从数据库加载图像的方法,但我想从 sdcard 加载图像并且只有图像的名称来自数据库

我搜索了很多,但找不到任何解决方案来解决我的问题...没有人可以帮助我解决这个问题吗?

【问题讨论】:

  • 使用惰性加载器,检查stackoverflow.com/questions/541966/…
  • 而不是 ImageView img=(ImageView)findViewById(R.id.list_item_img); 使用 ImageView img=(ImageView)view;
  • 谢谢我会测试它并说它是否有效..再次感谢
  • 我更改了它,但我的列表中没有看到任何更改..我认为视图活页夹根本没有做任何事情..我不知道为什么?...关于惰性列表。 .我不知道如何使用它:(

标签: android imageview simplecursoradapter


【解决方案1】:

我找到了这部分代码

esodaAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
             @Override
             public boolean setViewValue (View view, Cursor cursor, int columnIndex){
                 if (view.getId() == R.id.imageView1) {
                     ImageView IV=(ImageView) view;
                     int resID = getApplicationContext().getResources().getIdentifier(cursor.getString(columnIndex), "drawable",  getApplicationContext().getPackageName());
                     IV.setImageDrawable(getApplicationContext().getResources().getDrawable(resID));
                     return true;
                }
                 return false;
     } 

我认为我的代码状况有问题..你怎么看? 但是当我将代码更改为此

enter code here @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

        if (view.getId() == R.id.list_item_img) {
            // If the column is IS_STAR then we use custom view.
            int poet_id = cursor.getInt(columnIndex);

            String path = ClubCP.SDcardPath + "/temp/"+"9"+".jpg"; 
            File imgFile = new File(path);
            ImageView img=(ImageView)view;
            if(imgFile.exists())
        {
                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());                  

                img.setImageBitmap(myBitmap);
        }
            else                    
               Toast.makeText(Read.this,"no IMAGE IS PRESENT'",Toast.LENGTH_SHORT).show();


            img.setImageResource(R.drawable.music_album_header_vinyl);
        }

我的列表不再加载..只有列表布局重复,文本视图没有任何变化

【讨论】:

  • 您有没有想过并让它发挥作用?我正在尝试做同样的事情。我认为这一定是一个相当普遍的问题。我的 listview 行有一些文本和缩略图,但生成它的缩略图和源图像 not 存储在数据库中 - 只有文件路径在数据库中,图像在 SD 卡上(或任何可用的“存储”。)这样做是为了避免在数据库中存储巨大的“blob”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-28
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多