我是Android和Firestore的初学者我想计算数据库中字符串类型的投票数,我想用整数来计算它们,以便在条形图中绘制这些数字。
这是我的数据库
我附上 SeeVoters.java 文件
public class SeeVotes extends AppCompatActivity {
TextView tv,tv1,tv2;
private static final String TAG = "MainActivity";
private DocumentReference mUser;
private FirebaseFirestore mFirestore;
private CollectionReference mref;
private FirebaseAuth mAuth;
private FirebaseUser mCurrentUser;
int yescounter,nocounter;
int i = 0,j = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_see_votes);
tv=(TextView)findViewById(R.id.textView9);
tv1=(TextView)findViewById(R.id.textView13);
tv2=(TextView)findViewById(R.id.textView14);
Intent intent=getIntent();
final String vote=intent.getStringExtra("vote");
tv.setText(vote);
mFirestore = FirebaseFirestore.getInstance();
mAuth = FirebaseAuth.getInstance();
final String current_user_id = mAuth.getCurrentUser().getUid();
final String uname = mAuth.getCurrentUser().getDisplayName();
mUser = FirebaseFirestore.getInstance().collection("users").document(current_user_id);
mUser.collection("Vote").document(vote).collection("see_votes") .get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
{
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task)
{
if (task.isSuccessful())
{
for (DocumentSnapshot document : task.getResult())
{
// document.getId();
//final String votersname=document.getString("VotersName");
//final String votes=document.getString("Votes");
// if(votes.equals("Yes"))
// {
// i = i+1;
//
// }
// else if (votes.equals("No"))
// {
// j = j + 1;
// }
//
// yescounter = i;
// nocounter = j;
}
}
else
{
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
// Toast.makeText(this, "Yes : " + yescounter , Toast.LENGTH_SHORT).show();
// Toast.makeText(this, "No : " + nocounter , Toast.LENGTH_SHORT).show();
}
}
我能做些什么才能把它变成整数值...? 感谢..
答案 0 :(得分:0)
无法在Cloud Firestore中执行任何聚合查询。您需要找到另一种方法来获得总数。有几种选择: