Im working with android firebase and have managed to get the sum of a group of values from firebase and display it in logcat. Only i want to display this sum in a TextView, how would I do that? Here is my code
databaseReference.addValueEventListener(new ValueEventListener() { public void onDataChange(@NonNull DataSnapshot dataSnapshot) { heroList.clear(); int total = 0; for(DataSnapshot artistSnapshot: dataSnapshot.getChildren()){ Integer rating = artistSnapshot.child("editTextCalories").getValue(Integer.class); Hero hero = artistSnapshot.getValue(Hero.class); heroList.add(hero); total += Hero.getTotal(rating); } Log.d("Tag", total + ""); NameList adapter = new NameList(MainActivity.this, heroList); listView.setAdapter(adapter); }
Advertisement
Answer
You’ll need to set the value to the text view in onDataChange
, pretty much in the same spot where you already log it:
databaseReference.addValueEventListener(new ValueEventListener() { public void onDataChange(@NonNull DataSnapshot dataSnapshot) { heroList.clear(); int total = 0; for(DataSnapshot artistSnapshot: dataSnapshot.getChildren()){ Integer rating = artistSnapshot.child("editTextCalories").getValue(Integer.class); Hero hero = artistSnapshot.getValue(Hero.class); heroList.add(hero); total += Hero.getTotal(rating); } Log.d("Tag", total + ""); ((TextView)findViewById(R.id.myAwesomeTextView)).setText(""+total) NameList adapter = new NameList(MainActivity.this, heroList); listView.setAdapter(adapter); }