我是Java新手,正在构建一个Android应用程序。我想通过使用SimpleCursorAdapter将imageURI从本地SQLite DB添加到ListView。我读过不同的示例,但是,我仍然无法通过URI在ImageView中显示图像。每当我运行该应用程序并尝试检索ListView活动时,我的应用程序都会停止运行。谁可以帮我这个事?这是我的代码:
public class showproductdata extends AppCompatActivity {
static final String DB_NAME = "testDB";
static final String TB_PRODUCT = "productTB";
static final String COLUMN_PRODUCT_NAME = "product_name";
static final String COLUMN_PRODUCT_IMAGE_URI = "product_image_uri";
static final String[] FROM = new String[]{COLUMN_PRODUCT_NAME, COLUMN_PRODUCT_IMAGE_URI};
static final int[] TO = new int[]{R.id.txvShowProductName, R.id.imgDBImage};
SQLiteDatabase db;
SimpleCursorAdapter adapter;
Cursor cursor;
ListView lvProduct;
String strImageUri;
Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_showproductdata);
db = openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
cursor = db.rawQuery("SELECT * FROM " + TB_PRODUCT, null);
if (cursor != null) {
cursor.moveToFirst();
}
adapter = new SimpleCursorAdapter(this, R.layout.activity_productitem, cursor, FROM, TO, 0);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
@Override
public boolean setViewValue(View view, Cursor cursor, int index) {
if (view.getId() == R.id.imgDBImage) {
ImageView imgDBImage = findViewById(R.id.imgDBImage);
strImageUri = cursor.getString(cursor.getColumnIndex(COLUMN_PRODUCT_IMAGE_URI));
imageUri = Uri.parse(strImageUri);
imgDBImage.setImageURI(imageUri);
return true;
}
return false;
}
});
lvProduct = findViewById(R.id.lvProduct);
lvProduct.setAdapter(adapter);
}
}