正如标题所述。我在主活动布局内有一个ListView。一开始它是一个ConstraintLayout,但我决定将其设置为RelativeLayout,之后,row_item内的ImageView消失了,我无法放回去,我尝试了很多答案,但没有一个可以帮助我。
这里是ListAdapter代码:
public class ListAdapter extends ArrayAdapter<Product> {
private int resourceLayout;
private Context mContext;
private List<Product> items;
private AlertDialog.Builder alert;
EditText edittext;
public ListAdapter(Context context, int resource, List<Product> items) {
super(context, resource, items);
this.items = items;
this.resourceLayout = resource;
this.mContext = context;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
alert = new AlertDialog.Builder(mContext);
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(mContext);
v = vi.inflate(resourceLayout, null);
}
final Product p = getItem(position);
ImageView deleteImage = (ImageView) v.findViewById(R.id.delete_image);
deleteImage.setVisibility(View.VISIBLE);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edittext = new EditText(mContext);
edittext.setPaddingRelative(5,10,10,5);
edittext.setHint("New Quantity");
Log.d("DATABASE", " "+ position);
new AlertDialog.Builder(mContext)
.setTitle("Are you sure?")
.setView(edittext)
.setMessage("Do you want to change the quantity of product " + p.getName())
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String editedValue = edittext.getText().toString();
if (TextUtils.isEmpty(edittext.getText())){
Toast.makeText(mContext, "You need to specify a quantity", LENGTH_SHORT).show();
} else {
if (Integer.parseInt(editedValue) == 0 && Integer.parseInt(editedValue) > 100){
Toast.makeText(mContext, "Quantity should be greater than 0 and less than 100", LENGTH_SHORT).show();
}else {
p.setQuantity(Integer.parseInt(editedValue)); /// Ceva nu merge bine aici, schimba valoarea desi e 0
notifyDataSetChanged();
}
}
}
})
.setNegativeButton("No", null)
.show();
}
});
deleteImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(mContext)
.setIcon(R.drawable.del_button)
.setTitle("Are you sure?")
.setMessage("Do you want to delete the product " + p.getName() + " from you shopping cart ?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
items.remove(position);
notifyDataSetChanged();
}
})
.setNegativeButton("No", null)
.show();
}
});
if (p != null) {
TextView tt1 = (TextView) v.findViewById(R.id.product_name);
TextView tt2 = (TextView) v.findViewById(R.id.product_quantity);
TextView tt3 = (TextView) v.findViewById(R.id.product_price);
if (tt1 != null) {
tt1.setText(p.getName());
}
if (tt2 != null) {
tt2.setText(String.valueOf(p.getQuantity()));
}
if (tt3 != null) {
tt3.setText(String.valueOf(p.getPrice()*p.getQuantity()));
}
}
return v;
}
这是我的row_item布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:andorid="http://schemas.android.com/tools"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/product_name"
android:textSize="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Product Name"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/product_quantity"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/product_name"
android:text="Quantity:"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/product_name"
android:layout_marginLeft="45dp"
android:layout_centerInParent="true"
android:src="@android:drawable/ic_dialog_info"
android:text="Price"
android:textSize="26dp" />
<ImageView
android:id="@+id/delete_image"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="25dp"
android:clickable="true"
android:focusable="true"
andorid:src="@drawable/del_button" />
</RelativeLayout>
这是主要布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"
android:padding="15dp">
<Button
android:id="@+id/btn_scan"
android:layout_width="105dp"
android:layout_height="67dp"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:text="SCAN"
android:textAlignment="center"
android:textColor="@color/colorPrimary"
android:textSize="26dp"
/>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="570dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
android:layout_below="@id/btn_scan" />
</RelativeLayout>
正如我说的,在将主布局从“约束”更改为“相对”之前,一切都很好。不确定是什么问题,ImageView也具有onClick方法,该方法在显示AlertDialog后删除了单击的行。哪个工作正常。不知道是什么问题,我搜寻了答案,但没有一个帮助我。另外,这是我的照片文件夹:
[最有可能,问题出在android:src
属性中,因为它不支持只能通过app:srcCompat
加载的矢量可绘制对象>
因此,请将android:src
替换为app:srcCompat
;因此,您的列表项布局中的ImageView为:
<ImageView android:id="@+id/delete_image" android:layout_width="42dp" android:layout_height="42dp" android:layout_alignParentEnd="true" android:layout_centerVertical="true" android:layout_marginEnd="25dp" android:clickable="true" android:focusable="true" app:srcCompat="@drawable/del_button" />
并且在这种情况下,它可能会发出警告,要求您在android {...}下将以下代码段添加到gradle文件中,模块级别
android { defaultConfig { vectorDrawables.useSupportLibrary = true } }