如何从SQLiteDB中检索图像和文本以在列表视图中显示它

时间:2011-03-08 12:29:41

标签: android

我需要在SQLiteDB中存储数据,然后检索它以在Android中的ListView中显示它。在SQLiteDB和ListView中,数据将采用每行的图像和文本形式。

图像从SD卡存储到DB。我已将数据存储在数据库中;我只是不知道如何检索数据并显示它。

我的代码:

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 & 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<String>(ImageDBAct.this, android.R.layout.simple_list_item_1,results));
        db.close();
    }
}

我的DBAdapter如下:

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

}

1 个答案:

答案 0 :(得分:0)

更多代码会有用,但从我所看到的,您应该使用SimpleCursorAdapter而不是ArrayAdapter。 SimpleCursorAdapter适用于数据库,而ArrayAdapter更适合您的应用程序附带的静态数据,例如,以xml声明的字符串数组。

此外,要使SimpleCursorAdapter在项目中显示两个视图(在您的情况下为ImageView和TextView),请覆盖bindView()并使用该方法中提供的参数来创建视图。