How to recover the image from the local database and view in ListView (Android)

advertisements

[]I have a local database that is created using SQLite Manager and what I wanted to do is retrieve the data(item name, price, and image) from the database and display in a listview. I stored the image in drawable folder(kids.jpg) and in database I stored the name of the picture as string(kids).

The content of the listview will keep changing base on the condition of query.

The problem I am facing now is I can retrieve the name, price, and even the name of the image and list it out in the listview, but I don't know how to display the image in the listview.

I know that below code can be used to call drawable image from string but how am I going to insert the code to the listview?

int Image = getResources().getIdentifier("com.example.user.birthdaygiftpredictor:drawable/" + Gimage, null, null);
            gift_image.setImageResource(Image);

Below is my code for SimpleCursorAdapter

Cursor cursor = db.rawQuery("SELECT * FROM gift WHERE "+strValue+" OR hobby LIKE '%"+hobby+"%' OR gift_price<='"+budget+"' ORDER BY gift_price ASC ;",null);

    String[] from = new String[] {
            cursor.getColumnName(0),
            cursor.getColumnName(1),
            cursor.getColumnName(2),
            cursor.getColumnName(4),
            cursor.getColumnName(4),
            cursor.getColumnName(5),
            cursor.getColumnName(10)

    };
    int[] to = new int[] {
            R.id.gift_id,
            R.id.gift_name,
            R.id.gift_price,
            R.id.gift_image,
            R.id.image,
            R.id.gift_description,
            R.id.link

    };

SimpleCursorAdapter adapter = new SimpleCursorAdapter(
            Result_1_match.this, R.layout.view_gift_list, cursor, from, to);

 adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){

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

        if(view.getId() == R.id.gift_image){ // your ImageView id in xml
            DbHelper2 connect = new DbHelper2(Result_1_match.this);
            connect.openDataBase();
            SQLiteDatabase db = connect.getWritableDatabase();
            List<String> a = new ArrayList<>();

            Cursor c = db.rawQuery("your query;", null);
            int i=0;
            while (c.moveToNext()) {
                a.add(i,c.getString(0));
                i++;
            }
            ImageView imageView=(ImageView) view.findViewById(R.id.gift_image);
            int resID = getApplicationContext().getResources().getIdentifier(cursor.getString(columnIndex), "drawable",  getApplicationContext().getPackageName());
            imageView.setImageResource(resID);

            return true; //true because the data was bound to the view
        }
        return false;
    }
});

 list.setEmptyView(findViewById(R.id.imageView3));
    list.setAdapter(adapter);

Update: Finally I solved the problem and successfully listed out the image using Mukesh Rana concept. Above is the correct code. Thank you so much :)


From the google documentation for SimpleCursorAdapter, it says

SimpleCursorAdapter is an easy adapter to map columns from a cursor to TextViews or ImageViews defined in an XML file. You can specify which columns you want, which views you want to display the columns, and the XML file that defines the appearance of these views. Binding occurs in two phases. First, if a SimpleCursorAdapter.ViewBinder is available, setViewValue(android.view.View, android.database.Cursor, int) is invoked. If the returned value is true, binding has occured. If the returned value is false and the view to bind is a TextView, setViewText(TextView, String) is invoked. If the returned value is false and the view to bind is an ImageView, setViewImage(ImageView, String) is invoked. If no appropriate binding can be found, an IllegalStateException is thrown.

So you can easily override the corresponding setViewText(TextView,String) and setViewImage(ImageView,String) method. But If you still don't know how to do that , you can simply add a ViewBinder to your list adapter.

    adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
   /** Binds the Cursor column defined by the specified index to the specified view */
   public boolean setViewValue(View view, Cursor cursor, int columnIndex){
       if(view.getId() == R.id.your_image_view_id){ // your ImageView id in xml
            ImageView imageView=(ImageView) view;
                     int resID = getApplicationContext().getResources().getIdentifier(cursor.getString(columnIndex), "drawable",  getApplicationContext().getPackageName());
                     imageView.setImageDrawable(getApplicationContext().getResources().getDrawable(resID));
           return true; //true because the data was bound to the view
       }
       return false;
   }
});