Load Image in a custom list by SimpleCursorAdapter

别等时光非礼了梦想. 提交于 2019-12-23 05:50:12

问题


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


}

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

}

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?


回答1:


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

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

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



来源:https://stackoverflow.com/questions/15747380/load-image-in-a-custom-list-by-simplecursoradapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!