英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论