gpt4 book ai didi

java - 只打印特定信息到String

转载 作者:行者123 更新时间:2023-12-02 08:59:27 26 4
gpt4 key购买 nike

我有一个 Firebase 应用,可以使用数据快照从 Firebase 实时数据库返回用户 UID 和分数。

当我运行此代码时:

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";
String s;





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

leaderBoard();




}

public void leaderBoard(){
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());
s = s + snapshot.toString() + "\n";
score.setText(s);
}



}


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

}


});

}


}

我得到的结果是: enter image description here

有没有办法只返回键值和点值,而不返回随之而来的所有其他信息?

排行榜屏幕的当前状态: enter image description here

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".leaderboard">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.38">

<TextView
android:layout_width="420dp"
android:layout_height="150dp"
android:gravity="center"
android:text="Leaderboard!"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#0C40F1"
android:textSize="50dp"
android:textStyle="bold">

</TextView>
</RelativeLayout>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableRow>
<TextView
android:text="User ID"
android:padding="3dip"
android:layout_gravity="center"
android:textSize="20dp"/>
<TextView
android:text="Points"
android:layout_gravity="center"
android:gravity="right"
android:textSize="20dp"/>
</TableRow>
<TableRow>
<TextView
/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/tableText"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
</LinearLayout>



</ScrollView>

谢谢

最佳答案

您正在加载整个 Scores 节点,但显然只想显示每个子节点的单个属性的值。在这种情况下,您可以使用以下内容查找该属性的值:

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

@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCancelled", databaseError.toException());
}
});

关于java - 只打印特定信息到String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60269221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com