Java 是否替换布局文件夹中的视图和现有视图?
您好,我想在搜索结果时更改视图。 以下是我的java代码:Java 是否替换布局文件夹中的视图和现有视图?,java,android,xml,Java,Android,Xml,您好,我想在搜索结果时更改视图。 以下是我的java代码: public void searchProducts(View view, String search){ //Trying to get all the products, lists, transfers LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); Pr
public void searchProducts(View view, String search){
//Trying to get all the products, lists, transfers
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ProductHandler productHandler = new ProductHandler(context);
Product product = productHandler.getProductByBarCode(search);
if(product != null) {
LinearLayout layout = view.findViewById(R.id.searchLayout);
ViewGroup parent = (ViewGroup) layout.getParent();
View productView = inflater.inflate(R.layout.product_card, parent, false);
ImageView productImage = productView.findViewById(R.id.productTransferImageView);
TextView productName = productView.findViewById(R.id.productTransferNameTextView);
//image
try {
File imgFile = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), General.BASE_IMAGE_FILE_NAME + DBStatic.PRODUCT_TABLE_NAME + product.getId() + ".png");
if (imgFile.exists() && imgFile.length() != 0) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
productImage.setImageBitmap(myBitmap);
} else {
productImage.setImageResource(R.drawable.boxes);
}
} catch (Exception e) {
Log.d("Image", "Failed");
}
productName.setText(product.getName());
}
}
代码没有出现任何错误。但它不显示视图
下面是我想在函数中显示的产品卡
xml代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="?android:attr/selectableItemBackground"
android:foreground="?android:attr/selectableItemBackground"
>
<ImageView
android:id="@+id/productTransferImageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/boxes"/>
<TextView
android:id="@+id/productTransferNameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:textSize="16sp"
android:text="product"
android:textColor="@color/color3" />
我做错了什么。非常感谢您的帮助,谢谢