title: Updating specific firebase field using recyclerview button in android
body: <p>I'm developing a voting app using Android Studio in Java that uses <code>RecyclerView</code> to list all candidates from the Firebase database. I'm able to list all candidates, but can't implement the vote button to only update a specific candidate's total votes.</p>
  
  <p><img src="https://i.stack.imgur.com/mktgZ.png" alt="Database structure"></p>
  
  <p>The data is picked and displayed in a <code>RecyclerView</code> as follows:
  Candidates information in <code>RecyclerView</code>:
  <img src="https://i.stack.imgur.com/DaHRK.png" alt="Candidates information in RecyclerView."></p>
  
  <p>I need each time a user clicks on the vote button, the database <code>totalVotes</code> field is updated with a +1.</p>
  
  <p>MyAdapter code:</p>
  
  <pre><code>public class MyAdapter extends RecyclerView.Adapter&lt;MyAdapter.MyViewHolder&gt; {
  
      Context context;
      ArrayList &lt;Candidate&gt; candidates;
      private DatabaseReference mDatabase;
  
  
      public MyAdapter (Context c, ArrayList&lt;Candidate&gt; p){
          context = c;
          candidates =p;
      }
  
  
      @Override
      public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.cardview, parent, false));
      }
      @Override
      public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
          holder.name.setText(candidates.get(position).getFirstname());
          holder.party.setText(candidates.get(position).getParty());
          holder.category.setText(candidates.get(position).getCategory());
          Picasso.get().load(candidates.get(position).getImageurl()).into(holder.profilepic);
  
          holder.onClick(position);
      }
  
      @Override
      public int getItemCount() {
          return candidates.size();
      }
  
      class MyViewHolder extends RecyclerView.ViewHolder{
  
          TextView name, party, category;
          ImageView profilepic;
          Button vote;
  
          public MyViewHolder(View itemView) {
              super(itemView);
              name = (TextView) itemView.findViewById(R.id.name);
              party = (TextView) itemView.findViewById(R.id.party);
              profilepic = (ImageView) itemView.findViewById(R.id.profilepic);
              category = (TextView) itemView.findViewById(R.id.category);
              vote = (Button) itemView.findViewById(R.id.vote);
          }
  
          public void onClick(int position){
              vote.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
  
                  }
              });
          }
      }
  
  
  }
  </code></pre>
  
  
  
  <p>AllCandidates class that handles the adapter:</p>
  
  <pre><code>private DatabaseReference reference;
      private RecyclerView recyclerView;
      private ArrayList&lt;Candidate&gt; list;
      private ArrayList&lt;CandidateIMage&gt; listimage;
      private MyAdapter adapter;
      private DatabaseReference imagereference;
      FirebaseStorage storage;
      StorageReference storageReference;
  
      private static final String TAG = "AllCandidates";
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.all_candidates);
  
          //reference= FirebaseDatabase.getInstance().getReference().child("candidates");
          recyclerView = (RecyclerView) findViewById(R.id.myRecycler);
  
          recyclerView.setLayoutManager(new LinearLayoutManager(this));
          list = new ArrayList&lt;Candidate&gt;();
          listimage = new ArrayList&lt;CandidateIMage&gt;();
  
          reference = FirebaseDatabase.getInstance().getReference();
  
          Query presidentquery = reference.child("candidates").orderByChild("category").equalTo("President");
          presidentquery.addListenerForSingleValueEvent(new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                  if (dataSnapshot.exists()) {
                      for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                          Candidate p = dataSnapshot1.getValue(Candidate.class);
                          //String userId = dataSnapshot1.getKey();
                          //System.out.println("User id to be passed is: "+userId);
                          list.add(p);
                      }
                      adapter = new MyAdapter(AllCandidates.this, list);
                      recyclerView.setAdapter(adapter);
                  }
              }
              @Override
              public void onCancelled(DatabaseError databaseError) {
              }
          });
          };
      }
  </code></pre>
  
  
  
  <p>I had tried to add this code to the vote.onclicklostener but it is not working:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public void onClick(final int position){
              postRef = FirebaseDatabase.getInstance().getReference("candidates");
              vote.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      postRef.runTransaction(new Transaction.Handler() {
                          @Override
                          public Transaction.Result doTransaction(MutableData mutableData) {
                              Candidate p = mutableData.getValue(Candidate.class);
                              if (p == null) {
                                  return Transaction.success(mutableData);
                              } else {
                                  int fetched = p.getTotalVotes();
                                  fetched = fetched + 1;
  
                                  String userId = postRef.push().getKey();
                                  // Set value and report transaction success
                                  postRef.child(userId).child("totalVotes").setValue(fetched);
  
                                  //mutableData.setValue(p);
                                  return Transaction.success(mutableData);
                              }
                          }
                          @Override
                          public void onComplete(DatabaseError databaseError, boolean b,
                                                 DataSnapshot dataSnapshot) {
                              // Transaction completed
                              Log.d(TAG, "postTransaction:onComplete:" + databaseError);
                          }
                      });
                  }
  
  
              });
          }</code></pre>
  </div>
  </div>
  </p>
  
  
  
  <p>My Candidate m0del looks like this:</p>
  
  <pre><code>@IgnoreExtraProperties
  public class Candidate {
      private String firstname;
      private String lastname;
      private String party;
      private String category;
      private String candidateemail;
      private String imageurl;
      public Integer totalVotes;
  
      // Default constructor required for calls to
      // DataSnapshot.getValue(Candidate.class)
      public Candidate() {
      }
  
      public Candidate(String imageurl, String candidateemail, String firstname, String lastname, String party, String category, Integer totalVotes) {
  
          this.candidateemail = candidateemail;
          this.imageurl = imageurl;
          this.firstname = firstname;
          this.lastname = lastname;
          this.party = party;
          this.category = category;
          this.totalVotes = totalVotes;
      }
  
      public String getCandidateemail() {
          return candidateemail;
      }
  
      public void setCandidateemail(String candidateemail) {
          this.candidateemail = candidateemail;
      }
  
      public String getFirstname() {
          return firstname;
      }
  
      public void setFirstname(String firstname) {
          this.firstname = firstname;
      }
  
      public String getLastname() {
          return lastname;
      }
  
      public void setLastname(String lastname) {
          this.lastname = lastname;
      }
  
      public String getParty() {
          return party;
      }
  
      public void setParty(String party) {
          this.party = party;
      }
  
      public String getCategory() {
          return category;
      }
  
      public void setCategory(String category) {
          this.category = category;
      }
  
      public Integer getTotalVotes() {
          return totalVotes;
      }
  
      public void setTotalVotes(Integer totalVotes) {
          this.totalVotes = totalVotes;
      }
  
      public String getImageurl() {
          return imageurl;
      }
  
      public void setImageurl(String imageurl) {
          this.imageurl = imageurl;
      }
  </code></pre>