我正在尝试更新Firebase中的值并检索它。就像有投票功能的应用程序一样,但它只是垃圾firebase,并且无法正确更新。 我应该如何解决才能解决此问题?
提前谢谢!
公共类InProgress扩展了AppCompatActivity {
/*asking the user if they want to continue the program, if not the program will end*/
printf("\nDo you want to continue (y/n):");
scanf("%c", & ch);
if (ch == 'n' || ch == 'N')
break;
printf("\nThe program has been terminated\n");
}while (1);
return 0;
}
答案 0 :(得分:0)
您正在更新侦听器中的投票数,从而创建一个无限循环。设置一个侦听器,并在每次获取新值时更新TextView
,并且当您想更新该值时,只需在physicsVote
中进行设置,然后让您的侦听器完成其余的工作即可。
也仅侦听physics
更新,而不侦听整个数据库。
Long physicsVotes = 0L;
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
physicsVotes = dataSnapshot.getValue(Long.class);
physicsVoting.setText(physicsVotes);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
};
@Override
public void onStart() {
super.onStart();
reference.child("physics").addValueEventListener(listener);
}
@Override
public void onStop() {
super.onStop();
reference.child("physics").removeEventListener(listener);
}
@SuppressLint("SetTextI18n")
public void physicsVote(View view) {
reference.child("physics").setValue(++physicsVotes);
}