0

i am Beginner in Android and Firestore i want to count the number of votes which is of type string in the Database and i want to count them in integer in order to plot that numbers in bar graph.

this is my database

enter image description here

enter image description here

i am enclosing the SeeVoters.java file

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

    }
}

what can i do in order to get this into integer value...? Thanks..

Anurag Tiwari
  • 115
  • 1
  • 10

1 Answers1

1

It is not possible to carry out any aggregate queries in Cloud Firestore. You will need to find an alternative way to get the totals. There are several options:

  • Query all documents which voted 'yes' and count them in your client app, then do the same for all of the 'no' votes.
  • If you are not likely to receive more than one vote per second, you could create a cloud function which uses a transaction to update a single document. You may need to also write to the original document, to say that it has been counted. Functions need to be idempotent, so you need to ensure that votes aren't counted twice. See this great example.
  • Create a Cloud Function which writes each vote to something like BigQuery and list the votes there.
  • If your needs are more complex, consider piping the data through Dataflow (or other Apache Beam provider)
Jason Berryman
  • 4,760
  • 1
  • 26
  • 42