繁体   English   中英

如何在texview中显示来自firebase的一组数字的总和

[英]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);


        }

您需要将值设置为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);
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM