簡體   English   中英

如何使用DocumentSnapshot計算Firestore中的投票數

[英]How to count number of votes in Firestore using DocumentSnapshot

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

    }
}

我該怎么做才能將其轉換為整數值...? 謝謝..

在Cloud Firestore中無法執行任何匯總查詢。 您將需要找到另一種獲取總數的方法。 有幾種選擇:

  • 查詢所有投票為“是”的文檔,並在您的客戶端應用中對它們進行計數,然后對所有“否”票進行相同的操作。
  • 如果您每秒獲得的票數不太可能,則可以創建一個雲功能,該功能使用事務更新單個文檔。 您可能還需要寫原始文檔,以便說已被計算在內。 功能必須是冪等的,因此您需要確保不會計算兩次投票。 看到這個很好的例子
  • 創建一個Cloud Function,將每個投票寫入類似BigQuery的內容並在其中列出投票。
  • 如果您的需求更加復雜,請考慮通過Dataflow(或其他Apache Beam提供程序)來傳遞數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM