Harshal Kesharwani
Harshal Kesharwani

Reputation: 11

remove elements from listview in android

i have 2 arrays productname and price. my each row in listview contains productname,price and an image button remove. when i click remove button,the selected productname and price should be removed from my array as well as from my listview. Please guide me doing this,is very important for me. If there is another way of doing this plz let me know.i m student only!

this is my CartActivity

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);
        }
    }
}

this is my CartAdapter

public class CartAdapter extends ArrayAdapter {
List list = new ArrayList();
int ind,x=0;
public  CartAdapter(CartActivity context, int resource) {
    super(context, resource);
}
public void add(Cart object) {
    super.add(object);
    list.add(object);
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row;
    row = convertView;

    ContactHolder contactHolder;
    if(row == null)
    {
        LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.activity_cart_list_view,parent,false);
        contactHolder = new ContactHolder();
        contactHolder.tx_name =(TextView) row.findViewById(R.id.product_name);
        contactHolder.tx_price =(TextView) row.findViewById(R.id.price);

        contactHolder.cancelButton = (ImageButton) row.findViewById(R.id.cancel_button);


        row.setTag(contactHolder);
    }

    else
    {
        contactHolder = (ContactHolder)row.getTag();
    }

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

                Integer index = (Integer) view.getTag();
                list.remove(index.intValue());
            notifyDataSetChanged();

            }
        });


    Cart contacts = (Cart) this.getItem(position);
    contactHolder.tx_name.setText(contacts.getItemname());
    contactHolder.tx_price.setText(contacts.getPrice());
    return row;
}

static class ContactHolder
{
    TextView tx_name,tx_price;
    ImageButton cancelButton;
}
}

this is my Cart

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;
}
}

my each row in a listview contains this

Upvotes: 0

Views: 452

Answers (1)

Exigente05
Exigente05

Reputation: 2211

Just do this,

in your adapter class,

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

        }
    });

To remove from Activity class, better create an ArrayList first.

ArrayList<Cart> cardList = new ArrayList<Cart>();

add into 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
    }

now create a method to remove from ActivityCard arrayList,

//call this method from cancelimage click in adapter class
public void removeFromList(int position){
    cardList.remove(position);  //this will remove from activity arraylist
}

At the end calculate your bill from existing cartList data.

*** You can also create adapter class object with cardList just changing your constructor. If you do this then just call removeFromList on cancelimage click and put,

adapter.notifyDataSetChanged();

after

cardList.remove(position);

it will refresh your listview also.

Upvotes: 2

Related Questions