从Firebase的实时数据库中显示数据

huangapple go评论71阅读模式
英文:

Displaying data from Real-time database on Firebase

问题

我正在尝试显示数据库中的数据,但在这样做时遇到了问题。我试图显示的数据被保存为"total",并且保存为整数类型。我已经尝试将其保存为字符串和双精度浮点数,但仍然没有成功。理想情况下,我想要显示总数,然后在总数超过9的情况下显示一条消息。以下是我目前的代码:

public class Results extends AppCompatActivity {
    private DatabaseReference mDatabase;
    private FirebaseAuth fAuth;
    private Button btn;
    private TextView userR;
    private TextView num;
    private FirebaseUser currentUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_results);
        userR = (TextView) findViewById(R.id.userResults);
        btn = (Button) findViewById(R.id.generate);
        fAuth = FirebaseAuth.getInstance();
        currentUser = fAuth.getCurrentUser();
        mDatabase = FirebaseDatabase.getInstance().getReference().child("FAQ");
    }

    @Override
    public void onStart() {
        super.onStart();

        ValueEventListener scoreListener = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                faqinputs FAQInputs = dataSnapshot.getValue(faqinputs.class);
                userR.setText(FAQInputs.getTotal());
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        };

        mDatabase.addValueEventListener(scoreListener);
    }
}

"total" 被保存在一个名为 "FAQ" 的节点中,该节点位于一个名为 "User" 的节点下。

数据结构如下:
这是一张存储在特定节点下的一些数据的截图

以下是我用来创建 "FAQ" 节点的代码:

private void sendScoreData() {
    String currentUserID = fAuth.getUid();
    faqinputs FAQInputs = new faqinputs(questionOne, questionTwo, questionThree, questionFour, questionFive, questionSix, questionSeven, questionEight, questionNine, questionTen, total);
    mDatabase.child("Users").child(currentUserID).push().child("FAQ").setValue(FAQInputs);
}
英文:

I am trying to display data in my database but I'm having a problem doing so. The data I'm trying to display is saved as "total" and it is saved as an int. I have tried saving it as a string and double but still no luck. Ideally, I would like to display the total then display a message if the total is over the value 9. This is what I have so far.

public class Results extends AppCompatActivity {
    private DatabaseReference mDatabase;
    private FirebaseAuth fAuth;
    private Button btn;
    private TextView userR;
    private  TextView num;

private FirebaseUser currentUser;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_results);
    userR = (TextView) findViewById(R.id.userResults);

    btn = (Button) findViewById(R.id.generate);
    fAuth = FirebaseAuth.getInstance();
    currentUser = fAuth.getCurrentUser();

    mDatabase = FirebaseDatabase.getInstance().getReference().child("FAQ");
}

@Override
public void onStart() {
    super.onStart();

    ValueEventListener scoreListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            faqinputs FAQInputs = dataSnapshot.getValue(faqinputs.class);
            userR.setText(FAQInputs.getTotal());
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    };

    mDatabase.addValueEventListener(scoreListener);
}

}

"total" is saved in a "FAQ" node which is saved under a "User"

The data structure is as follows:
This is a screenshot some data stored under set nodes

The below data is the code I used to create the FAQ node.

  private void sendScoreData() {
        String currentUserID = fAuth.getUid();
        faqinputs FAQInputs = new faqinputs(questionOne, questionTwo, questionThree, questionFour, questionFive, questionSix, questionSeven, questionEight, questionNine, questionTen, total);
        mDatabase.child("Users").child(currentUserID).push().child("FAQ").setValue(FAQInputs);


    }

            }

答案1

得分: 1

要获取total属性的值,您需要将引用中的所有节点名称相加,就像以下代码行中所示:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference totalRef = rootRef.child("Users").child(uid).child("-M4YRj8nbobkuKurbHqC").child("FAQ").child("total");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        int total = dataSnapshot.getValue(Integer.class);
        Log.d("TAG", "total: " + total);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //不要忽略错误!
    }
};
totalRef.addListenerForSingleValueEvent(valueEventListener);

在logcat中的结果将会是:

total: 15

我看到total是一个数字,但是如果您将其保存为字符串,可以使用以下代码行:

String total = dataSnapshot.getValue(String.class);
英文:

To get the value of the total property, you have to add all node names in your reference, like in the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference totalRef = rootRef.child("Users").child(uid).child("-M4YRj8nbobkuKurbHqC").child("FAQ").child("total");
ValueEventListener valueEventListener = new ValueEventListener() {
	@Override
	public void onDataChange(DataSnapshot dataSnapshot) {
        int total = dataSnapshot.getValue(Integer.class);
		Log.d("TAG", "total: " + total);
	}

	@Override
	public void onCancelled(@NonNull DatabaseError databaseError) {
		Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
	}
};
totalRef.addListenerForSingleValueEvent(valueEventListener);

The result in the logcat will be:

total: 15

I see the total to be a number, however, if you save it as a String, use this line:

String total = dataSnapshot.getValue(String.class);

huangapple
  • 本文由 发表于 2020年4月10日 17:46:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/61137714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定