Home » Android » java – Only print specific information to String-Exceptionshub

java – Only print specific information to String-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I have a firebase app where I am able to use a datasnapshot to return a users UID and Score from my Firebase realtime database.

When I run this code:

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

            }


        });

    }


}

I get this as a result:
enter image description here

Is there anyway to only have the key value and points value returned as opposed to all of the other information that comes along with it?

The current state of the leaderboard screen:
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>

Thanks

How to&Answers:

You’re loading the entire Scores node, but apparently only want to show the value of a single property of each child. In that case you can look up the value of that property with something like:

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());
    }
});