0

我正在尝试在用户发送和删除好友请求时更新回收站视图..我正在使用 firebase firestore..当一个用户发送好友请求请求时显示在回收站视图中并且当请求被取消时,用户不会从列表中删除我正在使用以下代码,,

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");
                                            }
                                        });

删除时:

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;

我的回收站视图适配器:

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

}

视图持有人:

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

}

4

1 回答 1

0

在 notifyItemRemoved 和 notifyIteminserted 之后使用 adapter.notifyDataSetChanged() 我希望它对你有用。

于 2019-01-11T11:23:43.357 回答