RecyclerView中的两次数据



如何修复两次数据错误。

RecyclerView中滚动后,我遇到了两倍数据的问题。

这是我的活动:

apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<List<Contacts>> call = apiInterface.chat(getUserLogin,friendId);
call.enqueue(new Callback<List<Contacts>>() {
@Override
public void onResponse(Call<List<Contacts>> call, Response<List<Contacts>> response) {
contacts = response.body();
CustomAdapterOfChat adapter = new CustomAdapterOfChat(contacts, ChatActivity.this);
recyclerView.setAdapter(adapter);
size = String.valueOf(contacts.size());
}
@Override
public void onFailure(Call<List<Contacts>> call, Throwable t) {
Toast.makeText(getApplicationContext(), "Errorn"+t.toString(), Toast.LENGTH_LONG).show();
}
});

我的customAdapter ViewHolder

public class CustomAdapterOfChat extends RecyclerView.Adapter<CustomAdapterOfChat.MyViewHolder> {
private List<Contacts> contacts;
private Context context;
String getUserLogin;
public CustomAdapterOfChat(List<Contacts> contacts, Context context) {
this.contacts = contacts;
this.context = context;

}
@Override
public CustomAdapterOfChat.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items_of_chat, parent, false);
return new CustomAdapterOfChat.MyViewHolder(view);
}
@Override
public void onBindViewHolder(CustomAdapterOfChat.MyViewHolder holder, final int position) {
//Fetching id from shared preferences
SharedPreferences sharedPreferences;
sharedPreferences =context.getSharedPreferences(Constant.SHARED_PREF_NAME, Context.MODE_PRIVATE);
getUserLogin = sharedPreferences.getString(Constant.ID_SHARED_PREF, "");
Log.d("USER_ID",getUserLogin);
Log.d("FRIEND_ID",contacts.get(position).getFriendId());

holder.time.setText(contacts.get(position).getDateAndTime());
if(getUserLogin.equals(contacts.get(position).getFriendId())) {
holder.msg_of_me.setVisibility(View.INVISIBLE);
holder.msg_of_them.setText(contacts.get(position).getMessage());
holder.img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, UserProfileActivity.class);
intent.putExtra("id",contacts.get(position).getFriendId());
context.startActivity(intent);
}
});

Glide.with(context)
.load(Constant.BASE_URL+"y_chat/sign_up/"+contacts.get(position).getFriendName()+"/profile.png")
.error(R.drawable.error)
.into(holder.img);
}else{
holder.img.setVisibility(View.INVISIBLE);
holder.msg_of_them.setVisibility(View.INVISIBLE);
holder.msg_of_me.setText(contacts.get(position).getMessage());
}
}
@Override
public int getItemCount() {
return contacts.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView msg_of_me,msg_of_them,time;
ImageView img;
public MyViewHolder(View itemView) {
super(itemView);
msg_of_me = itemView.findViewById(R.id.message_text1);
msg_of_them = itemView.findViewById(R.id.message_text);
img = itemView.findViewById(R.id.img_of_them);
time = itemView.findViewById(R.id.message_time);
}
}
}

您正在从SharedPreferences获取数据,这是一项耗时的任务,但您可以通过这种方式编辑代码并检查输出。

public class CustomAdapterOfChat extends RecyclerView.Adapter {
private List<Contacts> contacts;
private Context context;
String getUserLogin;
public CustomAdapterOfChat(List<Contacts> contacts, Context context) {
this.contacts = contacts;
this.context = context;
//get user Id here.
SharedPreferences sharedPreferences;
sharedPreferences =context.getSharedPreferences(Constant.SHARED_PREF_NAME, 
Context.MODE_PRIVATE);
getUserLogin = sharedPreferences.getString(Constant.ID_SHARED_PREF, "");

}
@Override
public CustomAdapterOfChat.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items_of_chat, parent, false);
return new CustomAdapterOfChat.MyViewHolder(view);
}
@Override
public void onBindViewHolder(CustomAdapterOfChat.MyViewHolder holder, final int position) {
Log.d("USER_ID",getUserLogin);
Log.d("FRIEND_ID",contacts.get(position).getFriendId());

holder.time.setText(contacts.get(position).getDateAndTime());
if(getUserLogin.equals(contacts.get(position).getFriendId())) {
holder.msg_of_me.setVisibility(View.INVISIBLE);
holder.msg_of_them.setText(contacts.get(position).getMessage());
holder.img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, UserProfileActivity.class);
intent.putExtra("id",contacts.get(position).getFriendId());
context.startActivity(intent);
}
});

Glide.with(context)
.load(Constant.BASE_URL+"y_chat/sign_up/"+contacts.get(position).getFriendName()+"/profile.png")
.error(R.drawable.error)
.into(holder.img);
}else{
holder.img.setVisibility(View.INVISIBLE);
holder.msg_of_them.setVisibility(View.INVISIBLE);
holder.msg_of_me.setText(contacts.get(position).getMessage());
}

}
@Override
public int getItemCount() {
return contacts.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView msg_of_me,msg_of_them,time;
ImageView img;
public MyViewHolder(View itemView) {
super(itemView);
msg_of_me = itemView.findViewById(R.id.message_text1);
msg_of_them = itemView.findViewById(R.id.message_text);
img = itemView.findViewById(R.id.img_of_them);
time = itemView.findViewById(R.id.message_time);
}
}

最新更新