Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将文本视图动态添加到排行榜屏幕_Java_Android - Fatal编程技术网

Java 将文本视图动态添加到排行榜屏幕

Java 将文本视图动态添加到排行榜屏幕,java,android,Java,Android,我的应用程序中有一个排行榜屏幕,我想在其中添加每个用户的得分。 到目前为止,我已经能够从数据库中检索到分数,但我想在排行榜屏幕上显示这些分数 我有这段代码,但是for循环设置textView的文本,而不是显示每个分数,只显示循环的最后一个分数。是否仍然不覆盖textView,而是创建并显示多个textView package com.example.securityapp; import androidx.appcompat.app.AppCompatActivity; import and

我的应用程序中有一个排行榜屏幕,我想在其中添加每个用户的得分。 到目前为止,我已经能够从数据库中检索到分数,但我想在排行榜屏幕上显示这些分数

我有这段代码,但是for循环设置textView的文本,而不是显示每个分数,只显示循环的最后一个分数。是否仍然不覆盖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());

    }


});

        }
}

只要在循环再次启动时追加字符串即可

取全局变量字符串s=“”


s=s+Snapshot.toString()

您可以每次压缩字符串,但如果要保存它们,可以使用
sharedPrefenrece
。 首先,您可以将分数保存在SharedReference中,每次调用
onDataChanged
函数时,您都可以将newScore与以前的分数关联起来。 像这样:

SharedPreference sharedPreference = getSharedPreference("Scores" , MODE_PRIVATE); // in the top of database callback
在onDataChanged函数中:

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