两倍数据在RecyclerView中

huangapple go评论77阅读模式
英文:

Twice data in RecyclerView

问题

如何修复重复数据错误

`RecyclerView`内滚动后我遇到了一个问题即数据重复出现两次

这是我的Activity

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(), "Error\n"+t.toString(), Toast.LENGTH_LONG).show();
    }
});

我的自定义适配器 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) {

        //从共享首选项中获取ID
        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);
        }
    }
}
英文:

How can i fix twice data error.

After scrolling inside the RecyclerView, I am having a issue which is twice data.

Here is my Activity:

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

My customAdapter ViewHolder

public class CustomAdapterOfChat extends RecyclerView.Adapter&lt;CustomAdapterOfChat.MyViewHolder&gt; {
private List&lt;Contacts&gt; contacts;
private Context context;
String getUserLogin;
public CustomAdapterOfChat(List&lt;Contacts&gt; 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, &quot;&quot;);
Log.d(&quot;USER_ID&quot;,getUserLogin);
Log.d(&quot;FRIEND_ID&quot;,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(&quot;id&quot;,contacts.get(position).getFriendId());
context.startActivity(intent);
}
});
Glide.with(context)
.load(Constant.BASE_URL+&quot;y_chat/sign_up/&quot;+contacts.get(position).getFriendName()+&quot;/profile.png&quot;)
.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);
}
}
}

答案1

得分: 0

你正在从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;

        // 在这里获取用户ID。
        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);
        }
    }
}
英文:

You are getting data from SharedPreferences which is time consuming task however you can edit your code this way and check the output.

public class CustomAdapterOfChat extends RecyclerView.Adapter {
private List&lt;Contacts&gt; contacts;
private Context context;
String getUserLogin;
public CustomAdapterOfChat(List&lt;Contacts&gt; 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, &quot;&quot;);
}
@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(&quot;USER_ID&quot;,getUserLogin);
Log.d(&quot;FRIEND_ID&quot;,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(&quot;id&quot;,contacts.get(position).getFriendId());
context.startActivity(intent);
}
});
Glide.with(context)
.load(Constant.BASE_URL+&quot;y_chat/sign_up/&quot;+contacts.get(position).getFriendName()+&quot;/profile.png&quot;)
.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);
}
}

huangapple
  • 本文由 发表于 2020年4月10日 19:15:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/61139151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定