0

For the first time I want to change my value of the data in a recycler view holder, it is works, but once I click to change the value again in the same recycler view holder, it can't works and it still retrieve the old data. I tried the method like adapter.notifyDataSetChanged() but it seems like not work.

public class Admin_admittutorpage extends AppCompatActivity implements myadapter_tutorlist.OnNoteListener {

    //private static final String TAG = "Sometry" ;
    RecyclerView recview;
    myadapter_tutorlist adapter;

    public static ArrayList<User> allUser =new ArrayList<>();
    AlertDialog.Builder builder;

    DatabaseReference mroot ;
    private FirebaseAuth mFirebaseAuth;

    public void addUserInfo(User muser){
        allUser.add(muser);

    }

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

        recview = (RecyclerView)findViewById(R.id.admin_tutor_recyclerview);
        recview.setLayoutManager(new LinearLayoutManager(this));

        FirebaseRecyclerOptions<User> options = new FirebaseRecyclerOptions.Builder<User>()
            .setQuery(FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("role").equalTo("Tutor"),
            User.class).build();

        adapter = new myadapter_tutorlist(options, this);

        recview.setAdapter(adapter);

    }

    @Override
    protected void onStart(){
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop(){
        super.onStop();
        adapter.stopListening();
    }

    @Override
    public void onNoteClick(final int position) {
        //Log.d(TAG, "trytry" + allUser.get(position).getUsername());
        builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.dialog_message).setTitle(R.string.dialog_title);
        builder.setMessage("Do you want to delete or activate tutor account ?")
                .setCancelable(false)
                .setPositiveButton("Unactivate", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //  Action for 'NO' Button dialog.cancel();
                        Toast.makeText(getApplicationContext(), "you choose to unactivate tutor account", Toast.LENGTH_SHORT).show();
                        final String selectedEmail = allUser.get(position).getEmail();

                        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
                        DatabaseReference usersRef = rootRef.child("Users");
                        ValueEventListener eventListener = new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                for (DataSnapshot ds : snapshot.getChildren()) {
                                    String myemail = ds.child("email").getValue().toString();
                                    if(selectedEmail.equals(myemail)){
                                        if(allUser.get(position).getActive().equals(true)) {
                                            ds.getRef().child("active").setValue(false);
                                        }
                                        }
                                    }
                                }
                            }
                            @Override
                            public void onCancelled(@NonNull DatabaseError error) {

                            }
                        };
                        usersRef.addListenerForSingleValueEvent(eventListener);
                    }
                })
                .setNeutralButton("Delete", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Toast.makeText(getApplicationContext(), "you choose to delete tutor account", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("Activate", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //  Action for 'YES' Button
                        Toast.makeText(getApplicationContext(), "you choose to activate tutor account", Toast.LENGTH_SHORT).show();
                        if (allUser.get(position).getActive().equals(true)) {
                            Toast.makeText(getApplicationContext(), "The tutor account is already in active status", Toast.LENGTH_SHORT).show();
                        } else {
                            final String selectedEmail = allUser.get(position).getEmail();

                            DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
                            DatabaseReference usersdRef = rootRef.child("Users");
                            ValueEventListener eventListener = new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    for (DataSnapshot ds : dataSnapshot.getChildren()) {
                                        String myemail = ds.child("email").getValue().toString();
                                       
                                        if (selectedEmail.equals(myemail)) {
                                            if (allUser.get(position).getActive().equals(false)) {
                                                ds.getRef().child("active").setValue(true);
                                            } 
                                        }
                                    }
                                }

                                @Override
                                public void onCancelled(DatabaseError databaseError) {

                                }
                            };
                            usersdRef.addListenerForSingleValueEvent(eventListener);
                        }
                    }
                });
        //Creating dialog box
        AlertDialog alert = builder.create();
        //Setting the title manually
        alert.setTitle("Account Activation Alert");
        alert.show();
    }

}
horcrux
  • 4,954
  • 5
  • 24
  • 35
  • Check this, maybe you find it useful. https://stackoverflow.com/questions/31367599/how-to-update-recyclerview-adapter-data – SirGery May 17 '21 at 15:19
  • What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? Please respond with @AlexMamo – Alex Mamo May 17 '21 at 15:49
  • @AlexMamo Hi thanks for your help. My code is working well. But my problem is dont know where and how to implement the refresh for the recycleview in mycode. My void method addUserInfo will help me store all user data from onBindViewHolder – Candice May 17 '21 at 16:06
  • Does the update operation take place? Is the "active" property updated with "false"? – Alex Mamo May 17 '21 at 16:09
  • It's ok, I had tried to update my latest data to array and its work now. Thanks for your concern. – Candice May 18 '21 at 07:23

0 Answers0