【发布时间】:2017-09-27 23:01:12
【问题描述】:
我正在尝试从 imageView 获取图像路径字符串。
使用 SQLite db 中的字符串路径存储对 imageView 进行收费。
当我在图像路径之一以外的某些字段中修改我的 Db 项目时,我无法从 imageView 读回路径以正确更新数据库。因此,在任何类型的更新后,字段图像路径都会变为空。
在代码中,当我点击保存按钮时,我调用了 saveProduct() 方法:
private void saveProduct() {
// Read from input field
//Use trim to eliminate leading or trailing white space
String nameString = mNameEditText.getText().toString().trim();
String qtyString = mQtyEditText.getText().toString().trim();
String priceString = mPriceEditText.getText().toString().trim();
String mailString = mEmailEditText.getText().toString().trim();
String phoneString = mPhoneEditText.getText().toString().trim();
// Check if this is a new product or an existing one
if (mcurrentProdUri == null &&
TextUtils.isEmpty(nameString) && TextUtils.isEmpty(qtyString)
&& TextUtils.isEmpty(priceString)&& TextUtils.isEmpty(mailString)
&& TextUtils.isEmpty(phoneString)) {
return;
}
//Create a ContentValues object to populate the database
ContentValues values = new ContentValues();
values.put(InventoryContract.ProductEntry.COLUMN_PRODUCT_NAME, nameString);
values.put(InventoryContract.ProductEntry.COLUMN_PRODUCT_QTY, qtyString);
values.put(InventoryContract.ProductEntry.COLUMN_PRODUCT_PRICE, priceString);
values.put(InventoryContract.ProductEntry.COLUMN_EMAIL, mailString);
values.put(InventoryContract.ProductEntry.COLUMN_PHONE, phoneString);
values.put(InventoryContract.ProductEntry.COLUMN_PRODUCT_PIC, picPath);
//Determine if this is a new product or not by checking if mCurrentProdUri is null or not
if (mcurrentProdUri == null) {
Uri newUri = getContentResolver().insert(InventoryContract.ProductEntry.CONTENT_URI, values);
// check if newUri is null or not
if (newUri == null){
//Show an error toast message
Toast.makeText(this, "There has been an error inserting a new product", Toast.LENGTH_SHORT).show();
} else {
//Otherwise show a successful addition message
Toast.makeText(this, "A new product has been added!", Toast.LENGTH_SHORT).show();
}
} else {
//Otherwise if this is an existing product proceed updating it with new data
int rowsAffected = getContentResolver().update(mcurrentProdUri, values, null, null);
if (rowsAffected == 0) {
// If no rows were affected show error toast message
Toast.makeText(this, "There has been an error in updating the product", Toast.LENGTH_SHORT).show();
} else {
// Otherwise the update was successful and we can show the related Toast message
Toast.makeText(this, "The product has been updated!", Toast.LENGTH_SHORT).show();
}
}
}
我已经阅读了所有相关的帖子,但我真的不知道如何让方法读取 imageView 以获取相关的图像路径(或 Uri)
非常感谢您的支持
【问题讨论】:
-
一个
ImageView不知道图片来自哪里。您需要以其他方式记住此信息,例如通过活动或片段中的单独字段。 -
@CommonsWare 感谢您的快速回答。我使用 getLoaderManager().initLoader(EXISTING_PROD_LOADER, null, this); 在 UI 上加载数据,我想我必须在此处存储路径数据?
-
@CommonsWare!我已经解决了在 OnLoadFinished() 中添加一些逻辑的问题,正如这篇文章 stackoverflow.com/questions/14424624/… 中所建议的那样。感谢您让我走上正轨!