如何在 TextView 中显示来自 Firebase 的一组数字的总和。

huangapple 未分类评论49阅读模式
英文:

how can one display the sum of a group of numbers from firebase in a texview

问题

我正在使用 Android Firebase,已成功从 Firebase 获取一组值的总和并在 Logcat 中显示它。我想将这个总和显示在 TextView 中,我该如何做?以下是我的代码:

  1. databaseReference.addValueEventListener(new ValueEventListener() {
  2. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  3. heroList.clear();
  4. int total = 0;
  5. for(DataSnapshot artistSnapshot: dataSnapshot.getChildren()){
  6. Integer rating = artistSnapshot.child("editTextCalories").getValue(Integer.class);
  7. Hero hero = artistSnapshot.getValue(Hero.class);
  8. heroList.add(hero);
  9. total += Hero.getTotal(rating);
  10. }
  11. Log.d("Tag", total + "");
  12. NameList adapter = new NameList(MainActivity.this, heroList);
  13. listView.setAdapter(adapter);
  14. // 将 total 显示在 TextView 中
  15. TextView textView = findViewById(R.id.your_text_view_id); // 替换为你的 TextView ID
  16. textView.setText(String.valueOf(total));
  17. }
  18. });
英文:

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

  1. databaseReference.addValueEventListener(new ValueEventListener() {
  2. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  3. heroList.clear();
  4. int total = 0;
  5. for(DataSnapshot artistSnapshot: dataSnapshot.getChildren()){
  6. Integer rating = artistSnapshot.child("editTextCalories").getValue(Integer.class);
  7. Hero hero = artistSnapshot.getValue(Hero.class);
  8. heroList.add(hero);
  9. total += Hero.getTotal(rating);
  10. }
  11. Log.d("Tag", total + "");
  12. NameList adapter = new NameList(MainActivity.this, heroList);
  13. listView.setAdapter(adapter);
  14. }

答案1

得分: 1

你需要在onDataChange中为文本视图设置值,基本上就在你已经记录它的同一位置:

  1. databaseReference.addValueEventListener(new ValueEventListener() {
  2. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  3. heroList.clear();
  4. int total = 0;
  5. for(DataSnapshot artistSnapshot: dataSnapshot.getChildren()){
  6. Integer rating = artistSnapshot.child("editTextCalories").getValue(Integer.class);
  7. Hero hero = artistSnapshot.getValue(Hero.class);
  8. heroList.add(hero);
  9. total += Hero.getTotal(rating);
  10. }
  11. Log.d("Tag", total + "");
  12. ((TextView)findViewById(R.id.myAwesomeTextView)).setText("" + total);
  13. NameList adapter = new NameList(MainActivity.this, heroList);
  14. listView.setAdapter(adapter);
  15. }
  16. }
英文:

You'll need to set the value to the text view in onDataChange, pretty much in the same spot where you already log it:

  1. databaseReference.addValueEventListener(new ValueEventListener() {
  2. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  3. heroList.clear();
  4. int total = 0;
  5. for(DataSnapshot artistSnapshot: dataSnapshot.getChildren()){
  6. Integer rating = artistSnapshot.child("editTextCalories").getValue(Integer.class);
  7. Hero hero = artistSnapshot.getValue(Hero.class);
  8. heroList.add(hero);
  9. total += Hero.getTotal(rating);
  10. }
  11. Log.d("Tag", total + "");
  12. ((TextView)findViewById(R.id.myAwesomeTextView)).setText(""+total)
  13. NameList adapter = new NameList(MainActivity.this, heroList);
  14. listView.setAdapter(adapter);
  15. }

huangapple
  • 本文由 发表于 2020年5月29日 06:53:15
  • 转载请务必保留本文链接:https://java.coder-hub.com/62075848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定