1

I'am trying to make an android waiter ordering menu wherein I can take orders by clicking add/minus buttons in the products in my menu and presenting a summary of orders when I click another button. I created an custom listivew and arrayadapters. but i got stuck in the part wherein I should be able to collect the contents of the listitem in my listview where I clicked an add button. here are my codes. i want to be able to take the name price and the quantity of all the item that i added a value on.

public class coffee {
    public String name;
    public double price;
    public long itemID;
    public int qty;
    public int Icon;

    public coffee (String name, double price, int qty, int Icon){
        this.name = name;
        this.price = price;
        this.qty = qty;
        this.itemID = 0;
        this.Icon = Icon;
    }

    public String getName(){
        return name;
    }

    public double getPrice(){
        return  price;
    }

    public int getQty(){
        return qty;
    }

    public long getItemID(){
        return itemID;
    }

    public int getIcon(){
        return Icon;
    }

    public String toString(){
        return "ID: " + itemID + "Name: " + name + "Price: " + price + "Quantity: " + qty + "Image: "+ Icon;    }}

ArrayAdapter = coffee_adapter.java

public class coffee_adapter extends ArrayAdapter<coffee> {

public static class ViewHolder{

        TextView itemName;
        TextView itemPrice;
        TextView itemQty;
        ImageView itemImage;
        Button addButton;
        Button minusButton;
    }

    public coffee_adapter(Context context, ArrayList<coffee> cof) {
        super(context, 0, cof);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        final coffee coffeeList = getItem(position);

        final ViewHolder viewHolder;

        if(convertView == null ){

            viewHolder = new ViewHolder();

            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_row_layout,parent, false);

            viewHolder.itemImage = (ImageView) convertView.findViewById(R.id.itemImage);
            viewHolder.itemName = (TextView) convertView.findViewById(R.id.itemName);
            viewHolder.itemQty = (TextView) convertView.findViewById(R.id.quantity);
            viewHolder.itemPrice = (TextView) convertView.findViewById(R.id.itemPrice);
            viewHolder.addButton = (Button) convertView.findViewById(R.id.plusButton);
            viewHolder.minusButton = (Button) convertView.findViewById(R.id.minusButton);

            convertView.setTag(viewHolder);
        }
        else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.itemName.setText(coffeeList.getName());
        viewHolder.itemPrice.setText(String.valueOf(coffeeList.getPrice()));
        viewHolder.itemQty.setText(String.valueOf(coffeeList.getQty()));

        viewHolder.addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                coffeeList.qty ++;
                viewHolder.itemQty.setText(String.valueOf(coffeeList.qty));
                Toast.makeText(getContext(), "WTF " +coffeeList.qty , Toast.LENGTH_SHORT).show();

            }
        });

        viewHolder.minusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                coffeeList.qty --;
                if(coffeeList.qty < 0){
                    coffeeList.qty = 0;
                }
                viewHolder.itemQty.setText(String.valueOf(coffeeList.qty));
            }
        });
        return convertView;
    }
}

ListFragment

public class coffee_list extends ListFragment {

    private ArrayList<coffee> coffeeList;
    private coffee_adapter coffeeAdapter;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        coffeeList = new ArrayList<coffee>();

        coffeeList.add(new coffee("Qaldi Black(Americano)",75.00,0,R.drawable.testicon));

        coffeeAdapter = new coffee_adapter(getActivity(), coffeeList);

        setListAdapter(coffeeAdapter);


        getListView().setDivider(ContextCompat.getDrawable(getActivity(), android.R.color.darker_gray));
        getListView().setDividerHeight(1);
    }
}
mark.jon
  • 107
  • 12

1 Answers1

0

You'll want to call notifyDataSetChanged() to see the changes you make from clicking the buttons.

So something like:

viewHolder.itemQty.setText(String.valueOf(coffeeList.qty));
notifyDataSetChanged();

Adding on from your comment, if you wished to reset the list, in your List Fragment you can try:

private ArrayList<coffee> baseCoffeeList;
//...initialize baseCoffeeList like you did before in onActivityCreated

public void resetList() {
    setListAdapter(new coffee_adapter(getActivity(), baseCoffeeList));
}

OR (this I haven't tried):

In coffee_adapter.java:

public class coffee_adapter extends ArrayAdapter<coffee> {
    private ArrayList<coffee> coffeeList;
    public coffee_adapter(Context context, ArrayList<coffee> cof) {
        super(context, 0, cof);
        this.coffeeList = cof;
    }
    public reset(ArrayList<coffee> baseCoffeeList){
        coffeeList.clear();
        coffeeList.addAll(baseCoffeeList);
        notifyDataSetChanged();
    }
}
Bill
  • 4,506
  • 2
  • 18
  • 29
  • i see thx for your input sir. Is there also a way to reset the data to its original data? coz i want to reset it after I verify the ordered items. so i can take a new order . – mark.jon Aug 01 '16 at 18:21
  • I haven't tried the codes you gave just yet, because I'm planning to use it after the list of orders is confirmed/paid-out. This is the thing that I am currently puzzled on, for example I put a quantity count for the item(Qaldi Black Americano) and I want to store the data (Name, Price, Quantity wherein Quantity > 0) of that item to another arraylist(Order List). so that i can use to another activity for viewing all ordered items. I'm currently undergoing trial and errors, but I hope u can give me some ideas. PS. I'm planning on putting more items and categories to my app. thx for the help – mark.jon Aug 02 '16 at 16:08
  • you'll have to be more specific and ask a separate question with a working example, it's getting off topic from the original question – Bill Aug 02 '16 at 16:10
  • sorry about that. i already have put up a separated question, with my the current codes im working with. http://stackoverflow.com/questions/38750605/how-to-pass-the-data-s-from-1-arraylist-to-another-arraylist .. – mark.jon Aug 03 '16 at 18:00
  • hope you can still provide me with some ideas of yours. thank you. – mark.jon Aug 03 '16 at 18:14