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

huangapple go评论78阅读模式
英文:

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

问题

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

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);

            // 将 total 显示在 TextView 中
            TextView textView = findViewById(R.id.your_text_view_id); // 替换为你的 TextView ID
            textView.setText(String.valueOf(total));
        }
    });
英文:

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);


        }

答案1

得分: 1

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

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);
  }
}
英文:

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);
  }

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

发表评论

匿名网友

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

确定