我有一个项目列表,每个项目都有一个必须删除指定项目的按钮。我遇到的问题是,当我按下按钮时,它会删除所有项目。这是我正在使用的代码:
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View costumView = inflater.inflate(R.layout.cart_costum_raw, parent, false);
final JSONObject object = (JSONObject) getItem(position);
TextView productName = (TextView) costumView.findViewById(R.id.cartProductName);
TextView productQuantity = (TextView) costumView.findViewById(R.id.cartProductQuantity);
TextView productPrice = (TextView) costumView.findViewById(R.id.cartProductPrice);
ImageView productPicture = (ImageView) costumView.findViewById(R.id.cartProductImage);
Button remove = (Button) costumView.findViewById(R.id.removeButton);
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clear();
JSONObject serverResponse = null;
try {
RemoveProductRequest removeProductRequest = new RemoveProductRequest(object.get("user_id").toString() , object.get("product_id").toString() , object.get("dispensary_id").toString());
String response = removeProductRequest.doInBackground();
serverResponse = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
try
{
Glide.with(this.getContext()).load(object.getString("image")).into(productPicture);
productName.setText(object.get("name").toString());
productQuantity.setText(productQuantity.getText() + object.get("quantity").toString() + " grams");
productPrice.setText(productPrice.getText() + "$" + object.get("prod_price").toString());
}
catch (JSONException e)
{
e.printStackTrace();
}
return costumView;
}
这是我用来从列表中创建项目的适配器。
答案 0 :(得分:3)
您似乎在适配器内调用clear()
。它将清除所有适配器项。阅读文档:
https://developer.android.com/reference/android/widget/ArrayAdapter.html#clear()
如果您只想删除一个项目,请尝试拨打remove
:https://developer.android.com/reference/android/widget/ArrayAdapter.html#remove(T)