通过SimpleCursorAdapter在自定义列表中加载图像

时间:2013-04-01 16:36:34

标签: android imageview simplecursoradapter

我有一个自定义列表,我正在通过SimpleCursorAdapter加载数据。我的列表有一个imageview,我想每列的每一行都改变它的图像..我用了viewbilnder ..但没有得到任何东西.. 这是myadapter代码

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

}

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

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

1 个答案:

答案 0 :(得分:0)

我找到了这部分代码

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

我的列表不再加载..只有列表重复布局而没有任何文本视图更改