0

Everyone, I have a small problem which is the Toast here print every value added to the sum, the code must show only the last value then after that it will add the double of the existing values in the ArrayList.

The toast: Toast.makeText(BudgetList.this, "Totaltransaction:"+sum,Toast.LENGTH_LONG).show();

The code :

    FloatingActionButton fabtotal = (FloatingActionButton) findViewById(R.id.fabtotalbudg);
    fabtotal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick( View view1) {

            final Dialog dialog = new Dialog(context);
            dialog.setTitle("Budget details");
            final View view = getLayoutInflater().inflate(R.layout.custom_dialog, null);
            final ListView budlist2 = (ListView) view.findViewById(R.id.sublistView); //ListView to view the items from the database
             arrylist2 = new ArrayList<String>();
            arrayList3 = new ArrayList<String>();
            adapter2 = new ArrayAdapter<String>(BudgetList.this, android.R.layout.simple_list_item_1,arrylist2);
            adapter3 = new ArrayAdapter<String>(BudgetList.this, android.R.layout.simple_list_item_1,arrayList3);
            budlist2.setAdapter(adapter3);
            rootRef.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(final DataSnapshot dataSnapshot, String s) {

                    arrylist2.clear();
                    String value = dataSnapshot.child("budValue").getValue(String.class);
                    String budnameValue = dataSnapshot.child("budgname").getValue(String.class);
                    arrylist2.add(value);
                    arrayList3.add("Budget:"+budnameValue+"-Value: "+value);
                    dialog.setContentView(view);
                    dialog.setCancelable(true);
                    dialog.show();
                    adapter2.notifyDataSetChanged();
                    adapter3.notifyDataSetChanged();
                    if (arrylist2.size() >0)
                    {

                        for (int i = 0; i < arrylist2.size(); i++) { //iterate over the elements of the list
                            sum += Double.parseDouble(arrylist2.get(i));
                        }

                    }

                    else {
                        Toast.makeText(BudgetList.this, "No data", Toast.LENGTH_LONG).show();

                    }

                    Toast.makeText(BudgetList.this, "Total transaction:"+sum,Toast.LENGTH_LONG).show();


                }



                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                }
            });

        }
    });

And when I remove the Toast out of the FloatingAction button it will show zero value only.

Please help me if you can. Thank you.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user3604741
  • 33
  • 1
  • 8
  • please explain "remove the Toast out of the FloatingAction button" – Oğuzhan Döngül May 07 '17 at 13:14
  • Data loads from Firebase asynchronously. If you move the toast outside of the callback, it will display when the data hasn't been loaded yet. See my explanation of that behavior here: http://stackoverflow.com/questions/33203379/setting-singleton-property-value-in-firebase-listener – Frank van Puffelen May 07 '17 at 14:16

0 Answers0