user3628116
user3628116

Reputation: 45

how can i add items to an list view with custom adapter each time i click on the button?

what i am trying to do is something look like add to cart thing but on my android app , first of all i have add to cart activity which has all the values i need when i press the button i send my data as put extra to cartList Activity and it display on row and each time i press the button on some item it clear the array and display only one row ! but what i really need to do is when each time i press the button i want it to add on the previous items and display them all .. here is my add to cart activity :

public class AddToCart extends Activity{

    EditText quantity=null;
    TextView total=null;
    TextView name=null;
    TextView price=null;
    TextView ava=null;
    double totalprice=0.0;

    ArrayList<String> listOfItemNames = new ArrayList<String>();
    List<listClass>testList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.info);
        Intent test = getIntent();



        Intent intent = getIntent();
        final String itemprice = intent.getStringExtra("price");
        final String itemname = intent.getStringExtra("item");
        final String itemava = intent.getStringExtra("ava");
        final int imagehandler=intent.getIntExtra("image", 0);
        Log.e("image handler",imagehandler+"");
         name=(TextView)findViewById(R.id.textView2);
         price=(TextView)findViewById(R.id.textView4);
         total=(TextView)findViewById(R.id.textView7);
         ava=(TextView)findViewById(R.id.textView9);
        quantity=(EditText)findViewById(R.id.editText1);
        Button addtocart=(Button)findViewById(R.id.button1);
        ImageView imageview=(ImageView)findViewById(R.id.imageView1);

        name.setText(itemname);
        price.setText(itemprice);
        ava.setText(itemava);
        imageview.setImageResource(imagehandler);



            addtocart.setOnClickListener(new OnClickListener() {


                @Override
                public void onClick(View v) {
                    final String quantityvalue=quantity.getText().toString();
                    CharSequence currentavaiable=ava.getText();
                    int currentava=Integer.parseInt((String) currentavaiable);
                    Editable checked = quantity.getText();
                    String checkquantity=checked.toString();
                    Log.e("checked ",checked+"");
                    Log.e("check quantity",checkquantity);
                    try{

                if(checkquantity==null){
                    Toast.makeText(AddToCart.this,"please enter quantity for your item", Toast.LENGTH_LONG).show();
                }

                else{
                    int x=0;
                    double y=0.0;
//                  String xvalue=quantity.getText().toString();
                     x = Integer.parseInt(quantityvalue);
                     Log.e("x",x+"");
                     y = Double.parseDouble(itemprice);
                    Log.e("x",x+"");
                    Log.e("y",y+"");
                     totalprice=x*y;
                    total.setText(totalprice+"");
                    Toast.makeText(AddToCart.this,"your item added succesfully !", Toast.LENGTH_LONG).show();

                }

                Intent newintent=new Intent(AddToCart.this,CartList.class);
                newintent.putExtra("itemname",itemname);
                newintent.putExtra("itemprice",itemprice);
                newintent.putExtra("itemquantity",quantityvalue);
                newintent.putExtra("totalprice",totalprice);
                newintent.putExtra("image", imagehandler);

                startActivity(newintent);
                }//view
                    catch(Exception e){
                        e.printStackTrace();
                    }
                }
            });
    }
}

and here is cart list class :

public class CartList extends Activity {  
    ListView list;

    List<CartItems> detailsList = new ArrayList<CartItems>();
    ArrayList<String> listtest=new ArrayList <String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.cart_list);





        initializeComponents();

        detailsList = SetAdapterList();

        CustomAdapter adapter=new CustomAdapter(this,detailsList);

        list.setAdapter(adapter);




    }


    private void initializeComponents()
    {
        list=(ListView)findViewById(R.id.listView1);
    }







    private List<CartItems> SetAdapterList() 
    {
        // TODO Auto-generated method stub

        CartItems details;

         List<CartItems> detailsList = new ArrayList<CartItems>();

         try{
         Intent newintent = getIntent();
         final String itemname = newintent.getStringExtra("itemname");
        final String itemprice = newintent.getStringExtra("itemprice");
        final String itemquantity = newintent.getStringExtra("itemquantity");
        final double totalprice = newintent.getDoubleExtra("totalprice", 0.0);
        String totprice=String.valueOf(totalprice);
//      ArrayList <String> itemnames=new ArrayList<String>();
         listtest.add(itemname);
         Log.e("listtest",listtest+"");
        final int imagehandler=newintent.getIntExtra("image", 0);
        Log.e("image handler",imagehandler+"");
        for(int i = 0; i< listtest.size(); i++)
        {
        details = new CartItems(itemname,itemprice,itemquantity,totprice,imagehandler);
        detailsList.add(details);
        }


         }
         catch(Exception e){
             e.printStackTrace();
         }      
            return detailsList;
    }   

}

and here is my custom adapter :

public class CustomAdapter extends ArrayAdapter<CartItems> {

    Context context;
    List<CartItems>Clist = new ArrayList<CartItems>();

    public CustomAdapter(Context context, List<CartItems> detailsList) {
        super(context, R.layout.single_row, R.id.textView1, detailsList);
        // TODO Auto-generated constructor stub

        this.context=context;
        this.Clist = detailsList;


    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row=inflater.inflate(R.layout.single_row, parent,false);
        ImageView myimage=(ImageView) row.findViewById(R.id.imageView1);
        TextView items=(TextView)row.findViewById(R.id.textView1);
        TextView totalprice=(TextView)row.findViewById(R.id.textView2);



        items.setText(Clist.get(position).getItemName());
        totalprice.setText(Clist.get(position).getTotalPrice());
        myimage.setImageResource(Clist.get(position).getImage());

        return row;
    }

}

here is my itemcart class :

public class CartItems {

    private String itemName;
    private String itemPrice;
    private String Quantity;
    private String totalPrice;
    private int image;

    public CartItems(String itemname,String itemprice, String quantity, String totalprice2, int image){
        this.itemName=itemname;
        this.itemPrice=itemprice;
        this.Quantity=quantity;
        this.totalPrice=totalprice2;
        this.image=image;
    }
//getter and setters 
}

please can anyone help me?? i am so despaired !!

Upvotes: 1

Views: 1007

Answers (2)

Vikram Singh
Vikram Singh

Reputation: 1420

You are resetting you arraylist everyime. Instead you add item to it each time you select an item. Follow these steps:

1)change your setadapter function to return item, not itemlist.

private CartItems SetAdapterList() 
    {
        // TODO Auto-generated method stub

        CartItems details;
      try{
         Intent newintent = getIntent();
         final String itemname = newintent.getStringExtra("itemname");
        final String itemprice = newintent.getStringExtra("itemprice");
        final String itemquantity = newintent.getStringExtra("itemquantity");
        final double totalprice = newintent.getDoubleExtra("totalprice", 0.0);
        String totprice=String.valueOf(totalprice);
//      ArrayList <String> itemnames=new ArrayList<String>();
         listtest.add(itemname);
         Log.e("listtest",listtest+"");
        final int imagehandler=newintent.getIntExtra("image", 0);
        Log.e("image handler",imagehandler+"");
        for(int i = 0; i< listtest.size(); i++)
        {
        details = new CartItems(itemname,itemprice,itemquantity,totprice,imagehandler);

        }


         }
         catch(Exception e){
             e.printStackTrace();
         }      
            return details;
    }   

2) after getting this value, just add it to your arraylist. like this:

CartItems item = SetAdapterList();
detailsList.add(item);

Now, when you populate your listView, detailsList will be having all previously selected items too. Just clear detailsList when you want to reset.

Upvotes: 2

1) Add new items to your detailsList.

2) Call notifyDataSetChanged() for adapter.

Upvotes: 1

Related Questions