gpt4 book ai didi

android - 如何从android中的 ListView 中删除特定行?

转载 作者:行者123 更新时间:2023-11-30 01:09:32 25 4
gpt4 key购买 nike

解释: 我知道这个问题没有问。次。首先尝试了解我的问题。我有一个 ListView ,其中我使用 BaseAdapter 在我的列表行上显示动态项目,它不是固定的。我删除了包含在该行中的数据,但删除了该行。当我删除一行时,它的数据被完全删除但不是 ListView 行。我想从列表中删除特定行。

适配器

public class CartAdapter extends BaseAdapter {

private static final String TAG=CartAdapter.class.getSimpleName();

public Context context;
public List<CartItems> cartItemList;
public LayoutInflater inflater;
public AppCompatActivity activity;

public CartAdapter(Context context, List<CartItems> cartItemList,AppCompatActivity activity) {
this.context = context;
this.cartItemList = cartItemList;
this.activity=activity;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return this.cartItemList.size();
}

@Override
public Object getItem(int position) {
return this.cartItemList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}


private class CartHolder {
TextView txtItemName, txtItemPrice, txtQtyCount;
ImageButton btnImgDelete;
}

@Override
public View getView(final int position, View view, ViewGroup viewGroup) {

final CartHolder holder;
if (view == null) {
holder = new CartHolder();
view = inflater.inflate(R.layout.cart_items, null);

holder.txtItemName = (TextView) view.findViewById(R.id.txt_item_name);
holder.txtItemPrice = (TextView) view.findViewById(R.id.txt_item_price);
holder.txtQtyCount = (TextView) view.findViewById(R.id.txt_qty_count);

holder.btnImgDelete=(ImageButton)view.findViewById(R.id.btn_img_delete);

holder.btnImgDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
final AlertDialog.Builder alertDialog=new AlertDialog.Builder(activity,R.style.MyAlertDialogStyle);
alertDialog.setTitle("Delete");
alertDialog.setIcon(R.drawable.ic_action_cancel);
alertDialog.setMessage("Do you really want to delete item??");
alertDialog.setCancelable(true);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
cartItemList.remove(position);
notifyDataSetChanged();
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
alertDialog.show();
}
});
view.setTag(holder);
} else {
holder = (CartHolder) view.getTag();
}

CartItems cartItems = cartItemList.get(position);
holder.txtItemName.setText(cartItems.getItemName());
holder.txtItemPrice.setText(Util.priceTODollar(cartItems.getItemPrice()));
holder.txtQtyCount.setText("" + cartItems.getItemQty());


return view;
}
}

从 ListView 中删除项目之前的屏幕截图

enter image description here

从 ListView 中删除项目后的屏幕截图

enter image description here

在第二张截图中,第二个项目被删除,但白色背景颜色保持不变。

请帮我解决这个问题。

最佳答案

最后,我找到了解决方案。经过大量的努力和尝试。我不确定这是否是可行的解决方案。

从适配器中删除项目后,我设置处理程序并将 bundler 传递给 Message 对象,如下所示:

holder.btnImgDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
final AlertDialog.Builder alertDialog=new AlertDialog.Builder(activity,R.style.MyAlertDialogStyle);
alertDialog.setTitle("Delete");
alertDialog.setIcon(R.drawable.ic_action_cancel);
alertDialog.setMessage("Do you really want to delete item??");
alertDialog.setCancelable(true);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if(cartItemList.remove(cartItems)) {
notifyDataSetChanged();

Message message=new Message();
Bundle bundle=new Bundle();
bundle.putSerializable("KEY",(Serializable)cartItemList);
message.setData(bundle);
activity.uiHomeHandler.sendMessage(message);
}
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
alertDialog.show();
}
});

在 Activity 中我再次将 cartitemList 对象设置为 baseAdapter 并在删除项目后再次调用它

SuppressLint("HandlerLeak")
public class UiHandler extends Handler {
@Override
public void handleMessage(final Message msg) {
super.handleMessage(msg);
Bundle b=msg.getData();
cartItemList=(List)b.getSerializable("KEY");

mCartAdapter=new CartAdapter(getApplicationContext(),cartItemList,CartActivity.this);
lvCartItem.setAdapter(mCartAdapter);
Helper.getListViewSize(lvCartItem);
}
}

关于android - 如何从android中的 ListView 中删除特定行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38560064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com