在android中从listview中删除元素

在android中从listview中删除元素,android,listview,Android,Listview,我有两个阵列productname和price。我的listview中的每一行都包含productname、price和一个图像按钮remove。单击“删除”按钮时,所选的产品名称和价格应从我的阵列和列表视图中删除。请指导我做这件事,对我来说很重要。如果有其他方法,请告诉我。我是学生 这是我的活动 public class CartActivity extends Activity implements View.OnClickListener { CartAdapter contactAd

我有两个阵列productname和price。我的listview中的每一行都包含productname、price和一个图像按钮remove。单击“删除”按钮时,所选的产品名称和价格应从我的阵列和列表视图中删除。请指导我做这件事,对我来说很重要。如果有其他方法,请告诉我。我是学生

这是我的活动

public class CartActivity extends Activity implements View.OnClickListener {


CartAdapter contactAdapter;
ListView listView;
public String productname[]=new String[10];
public String price[]=new String[10];

int i=0,m;
CartActivity(String scanContent){
    this.content = scanContent;
}
public CartActivity() {}
String product,Price;
String pri,prod;
Intent intent;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);       
    listView = (ListView) findViewById(R.id.cart_list_view);

    contactAdapter = new CartAdapter(this,R.layout.activity_cart_list_view);
    listView.setAdapter(contactAdapter);

    intent = getIntent();
    productname = intent.getStringArrayExtra("productname");
    price = intent.getStringArrayExtra("price");

    for(int j=0;j<price.length;j++) {
        product = productname[j];
        Price = price[j];
        if (Price != null || product != null) {
            Cart contacts = new Cart(product, Price);
            contactAdapter.add(contacts);
        }
    }
}
这是我的车

public class Cart {
private  String itemname,price;
public Cart(String itemname,String price) {
    this.setItemname(itemname);
    this.setPrice(price);
}
public void setItemname(String itemname) {
    this.itemname = itemname;
}
public void setPrice(String price) {
    this.price = price;
}
public String getItemname() {
    return itemname;
}
public String getPrice() {
    return price;
}
}

只要这样做

在适配器类中

cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            list.remove(position);  //position is getView position
            // above line will remove from arraylist
            notifyDataSetChanged(); //this line reflects change in listview

        }
    });
要从活动类中删除, 最好先创建一个ArrayList

ArrayList<Cart> cardList = new ArrayList<Cart>();
现在创建一个从ActivityCard arrayList中删除的方法

if (Price != null || product != null) {
        Cart contacts = new Cart(product, Price);
        cardList.add(contacts);
        contactAdapter.add(contacts); // this line should be removed if you work with arrayList
    }
//call this method from cancelimage click in adapter class
public void removeFromList(int position){
    cardList.remove(position);  //this will remove from activity arraylist
}
最后,根据现有cartList数据计算账单

***您还可以使用cardList创建适配器类对象,只需更改构造函数。如果您这样做,只需在cancelimage上调用removeFromList,然后单击并放置

adapter.notifyDataSetChanged();
之后

卡片列表。移除(位置)

它还将刷新您的列表视图。

只需执行此操作

在适配器类中

cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            list.remove(position);  //position is getView position
            // above line will remove from arraylist
            notifyDataSetChanged(); //this line reflects change in listview

        }
    });
要从活动类中删除, 最好先创建一个ArrayList

ArrayList<Cart> cardList = new ArrayList<Cart>();
现在创建一个从ActivityCard arrayList中删除的方法

if (Price != null || product != null) {
        Cart contacts = new Cart(product, Price);
        cardList.add(contacts);
        contactAdapter.add(contacts); // this line should be removed if you work with arrayList
    }
//call this method from cancelimage click in adapter class
public void removeFromList(int position){
    cardList.remove(position);  //this will remove from activity arraylist
}
最后,根据现有cartList数据计算账单

***您还可以使用cardList创建适配器类对象,只需更改构造函数。如果您这样做,只需在cancelimage上调用removeFromList,然后单击并放置

adapter.notifyDataSetChanged();
之后

卡片列表。移除(位置)


它还将刷新您的列表视图。

先生,实际上前面的代码中有错误。请现在检查一下。是项正在从listview中删除,但我希望该元素从我通过intent接收的数组中删除。因为我想让这个数组生成Bill,请你再复习一遍这个问题,我想它又变了!对不起,先生。例如,我的两个数组都有5项。因此,listview中将显示5个项目。如果我们单击索引3上的删除按钮,那么它将从我的listview/arraylist中删除。但我希望从我的productname nd price阵列中删除index3元素。请帮助!先生,实际上我有些困惑,因为我是这个领域的新手。你能对我上传的代码进行修改并再次发送吗?请放心,我会这样做的,先生,事实上以前的代码中有错误。请现在检查一下。是项正在从listview中删除,但我希望该元素从我通过intent接收的数组中删除。因为我想让这个数组生成Bill,请你再复习一遍这个问题,我想它又变了!对不起,先生。例如,我的两个数组都有5项。因此,listview中将显示5个项目。如果我们单击索引3上的删除按钮,那么它将从我的listview/arraylist中删除。但我希望从我的productname nd price阵列中删除index3元素。请帮助!先生,实际上我有些困惑,因为我是这个领域的新手。你能修改我上传的代码并再次发送吗?我会的