0
我有一个列表视图的活动,列表视图包含一个imageview作为每行中的删除按钮。我已经将onclicklistener添加到imageview,但它不起作用。在列表视图上删除按钮事件不起作用
这是其中包含的ListView主要布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cart_list_view"
android:listSelector="@android:color/transparent"
android:cacheColorHint="@android:color/transparent">
</ListView>
这是由该ArrayList适配器处理的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_cart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.badr.eco.CartActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:contentDescription="row"
android:id="@+id/ImgProduct"
android:layout_width="150px"
android:layout_height="150px"
android:layout_margin="6dp"/>
<TextView
android:id="@+id/TitleProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="180px"
android:textSize="20sp"
android:layout_marginTop="10dp"
/>
<TextView
android:id="@+id/PriceProduct"
android:layout_below="@id/TitleProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="180px"
android:layout_marginTop="10dp"
android:textSize="20sp"
/>
<ImageView
android:id="@+id/delete_item_from_cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_delete_black_24dp"
android:layout_alignTop="@+id/PriceProduct"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
代码我的ArrayList适配器:
public class CartListAdapter extends ArrayAdapter<String> {
private ImageView img_prdtct;
private TextView title_prdct;
private TextView price_prdct;
private DBHelper Mydb;
private ImageView Img_delete;
private Context context;
private ArrayList<String> My_prdcts_List;
public CartListAdapter(Context context, int resource, ArrayList<String> objects) {
super(context, resource, objects);
this.context = context;
My_prdcts_List = objects;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_cart, parent, false);
String obj = My_prdcts_List.get(position);
String [] items = obj.split("/");
img_prdtct = (ImageView)rowView.findViewById(R.id.ImgProduct);
title_prdct = (TextView)rowView.findViewById(R.id.TitleProduct);
price_prdct = (TextView)rowView.findViewById(R.id.PriceProduct);
Img_delete = (ImageView) rowView.findViewById(R.id.delete_item_from_cart);
title_prdct.setText(items[0]);
price_prdct.setText(items[1] + "DH");
img_prdtct.setImageResource(Integer.parseInt(items[2]));
//Les evenements
img_prdtct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Clicked",Toast.LENGTH_SHORT).show();
//Log.d("Clicked", "deleteButton");
}
});
return rowView;
}
}
Acivity代码:
public class CartActivity extends AppCompatActivity {
private DBHelper Mydb;
private ArrayList<String> Array_List;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart_main);
Array_List = new ArrayList<>();
Mydb = new DBHelper(this);
Array_List = Mydb.getAllRows("cart");
CartListAdapter adapter = new CartListAdapter(this,0,Array_List);
ListView list = (ListView)findViewById(R.id.cart_list_view);
list.setAdapter(adapter);
}
}
最后这样的结果,但该事件的点击不起作用。 ListView
哪里是你的setTag? –
我发现了错误,我刚刚将img_prdtct.setOnClickListener替换为Img_delete.setOnClickListener –
“不起作用”是什么意思? –