skip to Main Content

Ok, so I have two modules in my app. One is for users, the other is for admin. the admin has to basically approve the user’s registration when a user registers themselves, for this I made a simple logic when a user registers they’re registered with an extra value userStatus = "0", and the admin when clicks the approve button the value changes to "1" and they’re allowed to log in. I’m using a Recycler View to show all the users from my database to the admin module but I wanna change a value that I’m not sure how to access,

below is my firebase database structure

(https://i.stack.imgur.com/LmYvR.png)
complete picture of firebase database for clarity

below is my code for the RecyclerView adapter class

package com.example.adminclient;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class AdapterRecycler extends RecyclerView.Adapter<AdapterRecycler.MyViewHolder> {


    Context context;
    ArrayList<UserData> list;
    FirebaseDatabase fDB = FirebaseDatabase.getInstance();
    DatabaseReference reference = fDB.getReference();



    public AdapterRecycler(Context context, ArrayList<UserData> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.users,parent,false);
        return new MyViewHolder(v);

    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        UserData userData = list.get(position);

        holder.firstName.setText(userData.getFirstName());
        holder.address.setText(userData.getAddress());
        holder.emailAddress.setText(userData.getEmailAddress());
        holder.userStatus.setText(userData.getUserStatus());

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{

        TextView firstName, address, emailAddress, userStatus;
        Button approveUser;
        FirebaseDatabase fDB = FirebaseDatabase.getInstance();
        DatabaseReference reference = fDB.getReference();


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            firstName = itemView.findViewById(R.id.firstNametv);
            address = itemView.findViewById(R.id.addresstv);
            emailAddress = itemView.findViewById(R.id.emailtv);
            approveUser = itemView. findViewById(R.id.approve);
            userStatus = itemView.findViewById(R.id.userStatustv);
           // String firstNamedb = firstName.getText().toString();



            approveUser.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    fDB.getReference().child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot snapshot) {

                                reference.child("Users").child(???).child("userStatus").setValue("1");

                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError error) {

                        }
                    });


                }
            });

        }


    }
}

Here’s a screenshot of Recycler View in app

Below is the code for Recycler View class

package com.example.adminclient;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class VerifyUsers extends AppCompatActivity {

    RecyclerView recyclerView;
    DatabaseReference databaseReference;
    AdapterRecycler adapterRecycler;
    ArrayList<UserData> list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify_users);

        recyclerView = findViewById(R.id.userVerify);
        databaseReference = FirebaseDatabase.getInstance().getReference("Users");
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));



        list = new ArrayList<>();
        adapterRecycler = new AdapterRecycler(this, list);
        recyclerView.setAdapter(adapterRecycler);

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                for (DataSnapshot dataSnapshot : snapshot.getChildren()){

                    UserData userData = dataSnapshot.getValue(UserData.class);
                    list.add(userData);
                }
                adapterRecycler.notifyDataSetChanged();


            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }
}

if there’s anyone out there that understands my situation and problem please help me out.

I tried what I could on ‘approve’ button onclicklistener by adding a value event listener to it and retrieving from firebase database reference for the child but I have no clue on how to move forward from here.

approveUser.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        fDB.getReference().child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                    reference.child("Users").child(???).child("userStatus").setValue("1");

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });


    }
});

Below here is my UserData class

package com.example.adminclient;

import android.location.Address;

public class UserData {
    String firstName;
    String address;
    String emailAddress;

    String userStatus;





    public UserData() {
    }
    
    public String getFirstName() {

        return firstName;
    }

    public String getAddress() {

        return address;
    }

    public String getEmailAddress() {

        return emailAddress;
    }

    public String getUserStatus() {
        return userStatus;
    }
}

2

Answers


  1. Try the following line of code.

    reference.child("Users").child(userData.getFirstName()).child("userStatus").setValue("1");
    
    Login or Signup to reply.
  2. The simplest solution would be to pass the entire UserData object from the onBindViewHolder to the bindData function that I have added inside the MyViewHolder class:

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        UserData userData = list.get(position);
        holder.bindData(userData); //👈
    }
    

    Once you have the UserData you can create a reference that point to the userStatus field and change the value:

    public static class MyViewHolder extends RecyclerView.ViewHolder{
        TextView firstName, address, emailAddress, userStatus;
        Button approveUser;
        DatabaseReference db = FirebaseDatabase.getInstance().getReference();
        DatabaseReference usersRef = db.child("Users");
    
    
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            firstName = itemView.findViewById(R.id.firstNametv);
            address = itemView.findViewById(R.id.addresstv);
            emailAddress = itemView.findViewById(R.id.emailtv);
            approveUser = itemView. findViewById(R.id.approve);
            userStatus = itemView.findViewById(R.id.userStatustv);
        }
    
    
        //             👇
        public void bindData(UserData userData) {
            firstName.setText(userData.getFirstName());
            address.setText(userData.getAddress());
            emailAddress.setText(userData.getEmailAddress());
            userStatus.setText(userData.getUserStatus());
    
            approveUser.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String firstName = userData.getFirstName();
                    usersRef.child(firstName).child("userStatus").setValue("1");
                    //                👆
                }
            });
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search