每个人,我都有一个小问题,就是Toast这里打印的每个值加上总和,代码必须只显示最后一个值 之后,它将在ArrayList中添加现有值的两倍。
祝酒词:
Toast.makeText(BudgetList.this, "Totaltransaction:"+sum,Toast.LENGTH_LONG).show();
代码:
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) {
}
});
}
});
当我从FloatingAction按钮中移除Toast时,它将仅显示零值。
如果可以,请帮助我。 谢谢。