由SimpleCursorAdapter自定义列表加载图片 [英] Load Image in a custom list by SimpleCursorAdapter

查看:671
本文介绍了由SimpleCursorAdapter自定义列表加载图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我一个SimpleCursorAdapter ..My列表中加载它的数据的自定义列表有,我想每list..I的每一行使用viewbilnder..but因此未得到任何改变形象的ImageView的..
这是myadapter code

I have a custom list that I am loading data in it by a SimpleCursorAdapter ..My list has an imageview that I want to change its image per each row of list..I used viewbilnder..but didnot get anything.. this is myadapter code

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

and this is 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;
    }

}

能有人帮我这个?
我搜索,找到一个方法来从数据库中加载图像,但我想从SD卡,只有图像的名字载入图像从数据库

Can some one helps me about this? I searched and find a way to load image from database but I want to load image from sdcard and only image's name is from database

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

I searched a lot but cant find any solution to fix my problem...No one can help me about this?

推荐答案

我发现code,这部分

I found this part of code

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

我觉得我的code的条件已经problem..what你觉得呢?
但是,当我改变我的code将该

I think condition of my code has problem..what do you think? But when I change my code to this

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

我的目录不无textviews任何改变加载anymore..and只列出重复布局

My list didn't load anymore..and only layout of list repeat without any change in textviews

这篇关于由SimpleCursorAdapter自定义列表加载图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆