How to retrieve image and text from SQLiteDB to display it in a listview
<p>I need to store data in SQLiteDB and later retrieve it to display it in a ListView in Android. In both SQLiteDB and the ListView, the data will take the form of image and text per row.</p>

<p>Images are stored from sdcard to DB. I have stored my data in the DB; I just don't know how to retrieve the data and display it.</p>

<p>My code:</p>

<pre><code>save.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        String DishName=edit_dish.getText().toString();
        db.open();
        db.insertImages(DishName, r_photo);
        db.close();
        Toast.makeText(ImageDBAct.this,"Data saved",Toast.LENGTH_SHORT).show();
    }
});

bt.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //---get all titles---
        db.open();
        Cursor c = db.getAllTitles();
        if (c.moveToFirst()) {
            do {          
                String firstName = c.getString(c.getColumnIndex("dish"));// here dish &amp; image are table field names
                ByteArrayInputStream imageStream = new ByteArrayInputStream(r_photo);
                Bitmap theImage = BitmapFactory.decodeStream(imageStream);
                //l_user_photo.setImageBitmap(theImage);
                //l_user_name.setText(firstName);
                results.add( firstName );
            } while (c.moveToNext());
        }
        setListAdapter(new ArrayAdapter&lt;String&gt;(ImageDBAct.this, android.R.layout.simple_list_item_1,results));
        db.close();
    }
}
</code></pre>

<p>My DBAdapter is as follows:</p>

<pre><code>public class DBAdapter {          
    private static final String DATABASE_NAME = "Dish.db";
    private static final int DATABASE_VERSION = 1;
    private static final String IMAGES_CREATE =
        "create table images (sno integer primary key autoincrement, "
        + "dish text not null, image BLOB, rating integer);";
    private final Context context; 
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(IMAGES_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS users");
            onCreate(db);
        }
    }   

    public DBAdapter open() throws SQLException {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        DBHelper.close();
    }

    public long insertImages(String uname, byte[] image) {
        ContentValues initialValues = new ContentValues();
        initialValues.put("username", uname);
        initialValues.put("image", image);
        return db.insert("images", null, initialValues);
    }

    public Cursor getAllTitles() {
        return db.query("images", new String[] {
            "sno", 
            "dish",
            "image"}, 
            null, 
            null, 
            null, 
            null, 
            null);
    }

    public Cursor getImages(String uname) {
        return db.query("images", new String[] {
            "sno", 
            "image"}, 
            "(username=" +uname+")", 
            null, 
            null, 
            null, 
            null);
    }

}
</code></pre>
