简体   繁体   English

动态地将 TextViews 添加到排行榜屏幕

[英]Dynamically add TextViews to a leaderboard screen

I have a leaderboard screen in my app where i want to add each users score.我的应用程序中有一个排行榜屏幕,我想在其中添加每个用户的分数。 So far I have been able to retrieve the scores from the the database but I want to show these scores on a leaderboard screen.到目前为止,我已经能够从数据库中检索分数,但我想在排行榜屏幕上显示这些分数。

I have this code, but the for loop sets the text of the textView and instead of each score being displayed, only the last score of the loop is displayed.我有这个代码,但是 for 循环设置了 textView 的文本,而不是显示每个分数,只显示循环的最后一个分数。 Is there anyway to not overwrite the textView and instead have multiple textViews created and displayed?无论如何不要覆盖textView,而是创建和显示多个textView?

package com.example.securityapp;

import androidx.appcompat.app.AppCompatActivity;

import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

import static java.lang.System.in;

public class leaderboard extends AppCompatActivity {

    DatabaseReference databaseUsers;
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    FirebaseAuth mAuth;
    TextView score;
    private static final String TAG = "leaderboard";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leaderboard);

        mAuth = FirebaseAuth.getInstance();
        FirebaseUser user = mAuth.getCurrentUser();
        score = (TextView) findViewById(R.id.tableText);


        database.getReference().child("Scores").addValueEventListener(new ValueEventListener() {
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        System.out.println("The score is: " + snapshot.toString());
                        score.setText(snapshot.toString());
                    }
                }


            @Override
            public void onCancelled(DatabaseError databaseError) {
             // Getting Post failed, log a message
             Log.w(TAG, "loadPost:onCancelled", databaseError.toException());

    }


});

        }
}

Just Append the string when the loop starts again当循环再次开始时追加字符串

Take A Global Variable String s=" ";取一个全局变量字符串 s="";

s = s + snapshoot.toString(); s = s + snapshoot.toString();

you can concat the string everytime but if you want to save them , you can use sharedPrefenrece .您可以每次都连接字符串,但如果您想保存它们,您可以使用sharedPrefenrece First you can save the scores in sharedPreference and everytime that the onDataChanged function is called , you can concat the newScore to the previous scores.首先,您可以将分数保存在 sharedPreference 中,每次调用onDataChanged函数时,您都可以将 newScore 连接到以前的分数。 like this :像这样 :

SharedPreference sharedPreference = getSharedPreference("Scores" , MODE_PRIVATE); // in the top of database callback

and in the onDataChanged function :在 onDataChanged 函数中:

sharedPreference.edit().putString("score" , sharedPreference.getString("score" , "") + " " + snapShot.toString);

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

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