0

I am working on ShoppingList project which holds information about amount of items and its price. The thing is that I kept them all into one array Shopping list<>. So now when I use AlertDialog Builder and input for example: 1 pc. Banana 25 eur.

I am not able to reach price, which I need to calculate final price of the products.

AlertDialog info:

enter image description here

How it looks so far:

enter image description here

This is a code which handles price amount:

if (id == R.id.action_add)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Fill out the fields");
        final EditText amount = new EditText(this);
        final EditText Item = new EditText(this);
        final EditText price = new EditText(this);
        amount.setHint("Input amount..");
        Item.setHint("Input item.. ");
        price.setHint("Input price.. ");
        LinearLayout layout = new LinearLayout(getApplicationContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        amount.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
        price.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
        layout.addView(amount);
        layout.addView(Item);
        layout.addView(price);
        builder.setView(layout);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                shoppingList.add("   " + amount.getText()+ " pc. " + "     "+Item.getText().toString()+ "     " + price.getText().toString() + " Dkk");
                Collections.sort(shoppingList);
                storeArrayVal(shoppingList, getApplicationContext());

                lv.setAdapter(adapter);
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.show();
        return true;

    }

Underneath, I am trying to make and function which should summarize all inputed amounts. This is what i have so far

if (id == R.id.action_sum) {

        Context context = getApplicationContext();
        CharSequence text = "Total amount here";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();



    }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Patrícia
  • 7
  • 5

2 Answers2

0

If you mean you want the user to input more than one amount and you got the total of them then you have to :

  1. Add variable like int total = 0; int the if statement or if the toast code is out then out the if statement
  2. In onClick dialogue you have are going to say total = total + total;
  3. Finally at the Toast code Toast toast = Toast.makeText(context, total, duration);

EDIT :

Filtering the list after removing it will need you 2 statements of code

  1. xItem = xItem.replaceFirst("\\d", ""); and that is for removing the first intger (the amount in your list case)
  2. xItem = xItem.replaceAll("[\\D]", ""); and that is for removing all the chars after the amount so you get the price only

that's all, but my recommendation for you is to catch the data before saving and use it not to save it and then reading and filtering it then use it

RESOURCES Removing whitespace from strings in Java Question and many more searching results for explination

String test = "1 banana 25";
    test = test.replaceFirst("\\d", "");
    test = test.replaceAll("[\\D]","");
    int num = Integer.parseInt(test);
    Log.i("test", "--" + String.valueOf(num));

This is a simple idea with it I tested it works or not, where num is the totalPrice that you want it's sum, it works very well just in case it is not working in your code that cuz of your code so I will need the whole code to compile it together

The Wrong with the Code :

First, the toast can not take the 2nd argument int as it represent and ID resource as this discussion says

so we will use String.valueOf(PriceTotal) then the total code that will be in R.id.actionsum will be :-

int PriceTotal = 0;
        for(String totalString : shoppingList){
            totalString = totalString.replaceFirst("\\d", "");
            totalString = totalString.replaceAll("[\\D]","");
            PriceTotal += Integer.parseInt(totalString);
        }

        Log.i("test Price =", String.valueOf(PriceTotal)); // delete it thats is for testing you can use it in app to test reslts 

        /*Context context = getApplicationContext();
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context,PriceTotal, duration);
        toast.show();*/
        Toast.makeText(this, String.valueOf(PriceTotal), Toast.LENGTH_LONG).show();  //thats is simpler it you want
Community
  • 1
  • 1
Dasser Basyouni
  • 3,142
  • 5
  • 26
  • 50
  • No its not that easy. I would like to use amounts from the list and summarize them. But after I save them into ShoppingList<> they are in format ["1 banana 25" , "2 ananas 30" ] and I need to retrieve from this list just that price amouts (25,30) and summarize them. Is it possible? – Patrícia Dec 22 '16 at 12:29
  • 1
    Then just create an object for the item. Add getter + setter and call `getPrice()` for each item you want to summarize. – beeb Dec 22 '16 at 12:42
  • Why you are doing ["1 banana 25" , "2 ananas 30"] this. Create a POJO(model) for Shopping List item. Create an arraylist like `ArrayList` – Ankita Shah Dec 22 '16 at 13:25
  • @Patricia you are looking for filtering the list after saving it, this is possible here is the code for you i will edit my answer – Dasser Basyouni Dec 22 '16 at 13:44
  • I tried it all nothing seems to work. Can someone help me with this on codeshare? so I don't need to post just a snippet of the code? I am getting really desperate – Patrícia Dec 22 '16 at 14:28
  • sure I can help you on codeshare but how this doesn't work I have tried it myself – Dasser Basyouni Dec 22 '16 at 14:31
  • @Patricia I am looking now and tring it on my Android Studio – Dasser Basyouni Dec 22 '16 at 14:49
  • sorry for that but can you give me the xml and java code so i can tried on mine Android Studio cuz if it crashes then the is a very small things to change, doing math with Strings is a normal thing but in case of converting them to ints or the string "1 + 0" wil be equal to "10", see my next answer edit – Dasser Basyouni Dec 22 '16 at 15:15
  • @DasserBasyouni I am uploading on GitHub just second – Patrícia Dec 22 '16 at 15:28
  • Never shared git respositori, hope it will work like this https://github.com/Pappy33/Shop_list – Patrícia Dec 22 '16 at 16:22
  • @Patricia I got it work and posting now the answer for you – Dasser Basyouni Dec 22 '16 at 17:08
0

Split the string by space characters. If you don't know how to, then read this. Splitting will give you an array of string with 3 elements.

string[0] is your item count, string[1] is the name and string[2] is the price.

string = "1 banana 25" will split to:

string[0] = 1, string[1] = banana, string[2] = 25.

Cast integers to integers and you are done.

But you can find a better way.

Community
  • 1
  • 1
Mean Coder
  • 304
  • 1
  • 12