借助保存在数据库中的路径从存储中检索图像并显示在 listView 中
Retrieve images from storage with the help of path saved in database and display in listView
我已经看到很多很多其他与此相关的问题,但 none 其中有帮助。
我有一个 table "item_table" 存储在数据库中,其中有 3 列,'ITEM'、'CATEGORY'、'IMAGE_PATH' 和 'PRICE'.
我可以在数据库中保存路径(路径是存储在 SDCard 中的图像)
。在 activity 上,我有一个微调器。当从微调器中选择一个类别时,列表视图应使用该项目图像的路径显示存储中的项目、价格和图像。
这是我的适配器 class.
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);
}
}
这是获取商品名称和价格的代码。
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);
}
}
但这对列表视图没有任何改变。请帮助我。
如果要在问题中进行任何编辑,请告诉我。
提前致谢。
更新您的 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);
}
我已经看到很多很多其他与此相关的问题,但 none 其中有帮助。
我有一个 table "item_table" 存储在数据库中,其中有 3 列,'ITEM'、'CATEGORY'、'IMAGE_PATH' 和 'PRICE'.
我可以在数据库中保存路径(路径是存储在 SDCard 中的图像)
。在 activity 上,我有一个微调器。当从微调器中选择一个类别时,列表视图应使用该项目图像的路径显示存储中的项目、价格和图像。 这是我的适配器 class.
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);
}
}
这是获取商品名称和价格的代码。
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);
}
}
但这对列表视图没有任何改变。请帮助我。 如果要在问题中进行任何编辑,请告诉我。 提前致谢。
更新您的 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);
}