Recycler view is not updating on realtime update

47
January 11, 2019, at 9:00 PM

i am trying to update recycler view when the user sends and removes friend request ..i am using firebase firestore..when one user sends the friend request request is shown in recycler view and when request is canceled the user is not removed from the list i am using following code,,

case ADDED:
                                    Log.d(TAG, "onEvent: " + dc.getDocument().toString());
                                    if (state.equals("recieved")) {
                                        mDb.collection("users").document(userId)
                                                .get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                                            @Override
                                            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                                Log.d(TAG, "onComplete: CheckingifTAskComplete");
                                                if (task.isSuccessful()) {
                                                    Log.d(TAG, "onComplete: tassk: succes :" + task.getResult());
                                                    DocumentSnapshot snapshot = task.getResult();
                                                    Users users = new Users(snapshot.getString("id"),
                                                            snapshot.getString("display_name"),
                                                            snapshot.getString("status"),
                                                            snapshot.getString("profile_image"),
                                                            snapshot.getString("thumb_image"));
                                                    Log.d(TAG, "onComplete: useradded" + users.toString());
                                                    mUSerRequestList.add(users);
                                                    adapter.notifyItemInserted(adapter.getItemCount()-1);
                                                }
                                            }
                                        }).addOnFailureListener(new OnFailureListener() {
                                            @Override
                                            public void onFailure(@NonNull Exception e) {
                                                Log.d(TAG, "onFailure: Failed");
                                            }
                                        });

on remove:

case REMOVED:
                                    Log.d("TAG", "Removed Msg: " + dc.getDocument());
                                    mDb.collection("users").document(userId)
                                            .get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                                        @Override
                                        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                            Log.d(TAG, "onComplete: CheckingifTAskComplete");
                                            if (task.isSuccessful()) {
                                                Log.d(TAG, "onComplete: tassk: succes :" + task.getResult());
                                                DocumentSnapshot snapshot = task.getResult();
                                                Users users = new Users(snapshot.getString("id"),
                                                        snapshot.getString("display_name"),
                                                        snapshot.getString("status"),
                                                        snapshot.getString("profile_image"),
                                                        snapshot.getString("thumb_image"));
                                                for (Users removeUser: mUSerRequestList) {
                                                    if (removeUser.getId().equals(users.getId())){
                                                        mUSerRequestList.remove(users);
                                                        adapter.notifyItemRemoved(mUSerRequestList.indexOf(users.getId()));
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    });
                                    break;

my recycler view adapter:

public class RequestsRecyclerViewAdapter extends RecyclerView.Adapter<RequestViewHolder> {
private static final String TAG = "RequestsRecyclerViewAda";
ArrayList<Users> mList;
private Context mainActivityContext;
static RequestViewHolder ho;

public RequestsRecyclerViewAdapter(Context mainActivityContext, ArrayList<Users> mList) {
    this.mList = mList;
    this.mainActivityContext = mainActivityContext;
}
@NonNull
@Override
public RequestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
    Log.d(TAG, "onCreateViewHolder: inflatingUserActivity");
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.snippet_user_request_single, parent, false);
    Log.d(TAG, "onCreateViewHolder: CallingUSersViewHolder");
    return new RequestViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final RequestViewHolder holder, final int position) {
    ho = holder;
    Log.d(TAG, "onBindViewHolder: RequestUSerGot: " + mList.get(position).toString());
    Log.d(TAG, "onBindViewHolder: " + holder.mName);
    holder.mName.setText(mList.get(position).getDisplay_name());
    Picasso.get()
            .load(mList.get(position).getThumb_image())
            .networkPolicy(NetworkPolicy.OFFLINE)
            .placeholder(R.drawable.ic_launcher_foreground)
            .into(holder.mProfileImage, new Callback() {
                @Override
                public void onSuccess() {
                }
                @Override
                public void onError(Exception e) {
                    Picasso.get()
                            .load(mList.get(position).getThumb_image())
                            .placeholder(R.drawable.ic_launcher_foreground)
                            .into(holder.mProfileImage);
                }
            });
    Log.d(TAG, "onBindViewHolder: picassoLoaded");

    addonClickListners(holder);
}
private void addonClickListners(RequestViewHolder holder) {
    holder.mAccept.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(mainActivityContext, "Added", Toast.LENGTH_SHORT).show();
        }
    });
    holder.mCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(mainActivityContext, "Canceld", Toast.LENGTH_SHORT).show();
        }
    });
}

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

}

view holder:

public class RequestViewHolder extends RecyclerView.ViewHolder {
public CircleImageView mProfileImage;
public TextView mName;
public ImageButton mAccept, mCancel;
public RequestViewHolder(@NonNull View itemView) {
    super(itemView);
    mProfileImage = itemView.findViewById(R.id.requestFPRofileImage);
    mName = itemView.findViewById(R.id.requestFName);
    mAccept = itemView.findViewById(R.id.requestFAccept);
    mCancel = itemView.findViewById(R.id.requestFCancel);
}

}

Answer 1

use adapter.notifyDataSetChanged() after notifyItemRemoved and notifyIteminserted I hope its work for you.

READ ALSO
Confusion in Geocoder Class

Confusion in Geocoder Class

I just want to launch my application which is using Reverse Geocoding with the help of Geocoder Class but I want to ask whether this cause any problem further?

35
where is the path of my react native static server

where is the path of my react native static server

I'm trying to run a static server in my app developed with react native, with the packages react-native-static-server and react-native-fs

50
Read/write ble characteristics from/to particular service

Read/write ble characteristics from/to particular service

I have several services with different UUIDs, but with the same characteristics UUIDsI'm not sure what characteristic will be got if I call rxBleConnection

63
Updating ExpandableListView inside a fragment from another Activity (not the parent Activity)

Updating ExpandableListView inside a fragment from another Activity (not the parent Activity)

In my case i have two Activities that contains a ViewPager with the relative FragmentsSo the situation is something like that:

59