借助保存在数据库中的路径并在listView中显示,从存储中检索图像

时间:2016-11-15 09:23:20

标签: android sqlite listview android-database

我见过许多与此相关的问题,但没有一个问题有帮助。

我有一个表“item_table”存储在数据库中,其中有3列,'ITEM','CATEGORY','IMAGE_PATH'和'PRICE'。

我能够在DB中保存路径(路径是存储在SD卡中的图像)

。在一项活动中,我有一个旋转器。当从微调器中选择一个类别时,列表视图应使用该项目图像的路径显示存储中的项目,价格和图像。 这是我的适配器类。

CustomListAdapter.Java

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.widget.ImageView;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;

public class CustomListAdapter extends ResourceCursorAdapter {


public CustomListAdapter(Context context, int layout, Cursor c) {
    super(context, layout, c);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView itemName = (TextView)view.findViewById(R.id.itemName_lbl);
    String name_string = cursor.getString(cursor.getColumnIndex("item"));
    int resIdName = context.getResources().getIdentifier(name_string,"string",context.getPackageName());
    itemName.setText(resIdName);

    TextView price = (TextView)view.findViewById(R.id.price_lbl);
    String price_string = cursor.getString(cursor.getColumnIndex("price"));
    int resPriceName = context.getResources().getIdentifier(price_string,"string",context.getPackageName());
    price.setText(resPriceName);

}
}

这是获取itemName和price的代码。

 public Cursor getList () {
    SQLiteDatabase db = myDb.getReadableDatabase();

    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String s = spinner.getSelectedItem().toString();

    String[] select = {"_id","item","price"};
    String table = "item_table";
    String where = "category = '"+s+"' ";

    qb.setTables(table);
    Cursor c = qb.query(db,select,where,null,null,null,null);
    c.moveToFirst();

    return c;
}

有了这个我能够得到物品的名称和价格,但我无法在路径的帮助下获得这些物品的图像。

这是我到目前为止所尝试过的。

 ArrayList<String> images = new ArrayList<String>();
    {
    String spinnerCategory = spinner.getSelectedItem().toString();
        String selectQuery = "SELECT image_path from item_table where category = '"+spinnerCategory+"' ";
        SQLiteDatabase db = myDb.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                images.add(cursor.getString(0));
            } while (cursor.moveToNext());
            cursor.close();
        }

        File imgFile = new File(String.valueOf(images));
        if (imgFile.exists()) {
            Bitmap myBitmap = BitmapFactory.decodeFile(String.valueOf(images));
            //Drawable d = new BitmapDrawable(getResources(), myBitmap);
            ImageView myImage = (ImageView) findViewById(R.id.itemImage_icon);
            myImage.setImageBitmap(myBitmap);

        }
    }

但是这在listview中没有任何改变。请帮助我。 如果要在问题中进行任何编辑,请告诉我。 谢谢。

1 个答案:

答案 0 :(得分:0)

更新你的getList()方法

public Cursor getList () {
SQLiteDatabase db = myDb.getReadableDatabase();

SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

String s = spinner.getSelectedItem().toString();

String[] select = {"_id","item","price","image_path"};
String table = "item_table";
String where = "category = '"+s+"' ";

qb.setTables(table);
Cursor c = qb.query(db,select,where,null,null,null,null);
c.moveToFirst();

return c;
}

更新你的适配器

@Override
public void bindView(View view, Context context, Cursor cursor) {

TextView itemName = (TextView)view.findViewById(R.id.itemName_lbl);
String name_string = cursor.getString(cursor.getColumnIndex("item"));
int resIdName = context.getResources().getIdentifier(name_string,"string",context.getPackageName());
itemName.setText(resIdName);

TextView price = (TextView)view.findViewById(R.id.price_lbl);
String price_string = cursor.getString(cursor.getColumnIndex("price"));
int resPriceName = context.getResources().getIdentifier(price_string,"string",context.getPackageName());
price.setText(resPriceName);

ImageView myImage = (ImageView) view.findViewById(R.id.itemImage_icon);
String filePath = cursor.getString(cursor.getColumnIndex("image_path"));
Glide.with(context).load(filePath).into(myImage);

}