RecyclerView items duplicate and constantly changing
up vote
9
down vote
favorite
What's Happening:
The list (RecyclerView)
is mixing up the data when I scroll.
I.E when I scroll back up after scrolling down, some of the list items are repeated, not displaying proper content.
package jamesnguyen.newzyv2.UI_update;
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import jamesnguyen.newzyv2.R;
import jamesnguyen.newzyv2.RSS_Processcors.RssItem;
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.FeedViewHolder>
private static List<RssItem> items = null;
private static Context context;
public RVAdapter(Context context, List<RssItem> items)
this.items = items;
this.context = context;
public static List<RssItem> getItems()
return items;
@Override
public RVAdapter.FeedViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
return new FeedViewHolder(v);
@Override
public void onBindViewHolder(RVAdapter.FeedViewHolder holder, int position)
FeedViewHolder.getTitle().setText(items.get(position).getTitle());
FeedViewHolder.getPubDate().setText(items.get(position).getPubDate());
//FeedViewHolder.getDescription().setText(items.get(position).getDescription());
@Override
public long getItemId(int id)
return id;
@Override
public int getItemCount()
return items.size();
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView)
super.onAttachedToRecyclerView(recyclerView);
public static class FeedViewHolder extends RecyclerView.ViewHolder
private static CardView cv;
private static TextView title;
private static TextView pubDate;
private static TextView description;
FeedViewHolder(View itemView)
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
title = (TextView) itemView.findViewById(R.id.title);
pubDate = (TextView) itemView.findViewById(R.id.pubDate);
//description = (TextView) itemView.findViewById(R.id.description);
public static TextView getTitle()
return title;
public static TextView getPubDate()
return pubDate;
public static TextView getDescription()
return description;
I believe maybe some works have to be done with the recycling process of RecyclerView but nothing i tried seesm to work.
android android-recyclerview
add a comment |
up vote
9
down vote
favorite
What's Happening:
The list (RecyclerView)
is mixing up the data when I scroll.
I.E when I scroll back up after scrolling down, some of the list items are repeated, not displaying proper content.
package jamesnguyen.newzyv2.UI_update;
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import jamesnguyen.newzyv2.R;
import jamesnguyen.newzyv2.RSS_Processcors.RssItem;
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.FeedViewHolder>
private static List<RssItem> items = null;
private static Context context;
public RVAdapter(Context context, List<RssItem> items)
this.items = items;
this.context = context;
public static List<RssItem> getItems()
return items;
@Override
public RVAdapter.FeedViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
return new FeedViewHolder(v);
@Override
public void onBindViewHolder(RVAdapter.FeedViewHolder holder, int position)
FeedViewHolder.getTitle().setText(items.get(position).getTitle());
FeedViewHolder.getPubDate().setText(items.get(position).getPubDate());
//FeedViewHolder.getDescription().setText(items.get(position).getDescription());
@Override
public long getItemId(int id)
return id;
@Override
public int getItemCount()
return items.size();
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView)
super.onAttachedToRecyclerView(recyclerView);
public static class FeedViewHolder extends RecyclerView.ViewHolder
private static CardView cv;
private static TextView title;
private static TextView pubDate;
private static TextView description;
FeedViewHolder(View itemView)
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
title = (TextView) itemView.findViewById(R.id.title);
pubDate = (TextView) itemView.findViewById(R.id.pubDate);
//description = (TextView) itemView.findViewById(R.id.description);
public static TextView getTitle()
return title;
public static TextView getPubDate()
return pubDate;
public static TextView getDescription()
return description;
I believe maybe some works have to be done with the recycling process of RecyclerView but nothing i tried seesm to work.
android android-recyclerview
add a comment |
up vote
9
down vote
favorite
up vote
9
down vote
favorite
What's Happening:
The list (RecyclerView)
is mixing up the data when I scroll.
I.E when I scroll back up after scrolling down, some of the list items are repeated, not displaying proper content.
package jamesnguyen.newzyv2.UI_update;
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import jamesnguyen.newzyv2.R;
import jamesnguyen.newzyv2.RSS_Processcors.RssItem;
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.FeedViewHolder>
private static List<RssItem> items = null;
private static Context context;
public RVAdapter(Context context, List<RssItem> items)
this.items = items;
this.context = context;
public static List<RssItem> getItems()
return items;
@Override
public RVAdapter.FeedViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
return new FeedViewHolder(v);
@Override
public void onBindViewHolder(RVAdapter.FeedViewHolder holder, int position)
FeedViewHolder.getTitle().setText(items.get(position).getTitle());
FeedViewHolder.getPubDate().setText(items.get(position).getPubDate());
//FeedViewHolder.getDescription().setText(items.get(position).getDescription());
@Override
public long getItemId(int id)
return id;
@Override
public int getItemCount()
return items.size();
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView)
super.onAttachedToRecyclerView(recyclerView);
public static class FeedViewHolder extends RecyclerView.ViewHolder
private static CardView cv;
private static TextView title;
private static TextView pubDate;
private static TextView description;
FeedViewHolder(View itemView)
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
title = (TextView) itemView.findViewById(R.id.title);
pubDate = (TextView) itemView.findViewById(R.id.pubDate);
//description = (TextView) itemView.findViewById(R.id.description);
public static TextView getTitle()
return title;
public static TextView getPubDate()
return pubDate;
public static TextView getDescription()
return description;
I believe maybe some works have to be done with the recycling process of RecyclerView but nothing i tried seesm to work.
android android-recyclerview
What's Happening:
The list (RecyclerView)
is mixing up the data when I scroll.
I.E when I scroll back up after scrolling down, some of the list items are repeated, not displaying proper content.
package jamesnguyen.newzyv2.UI_update;
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import jamesnguyen.newzyv2.R;
import jamesnguyen.newzyv2.RSS_Processcors.RssItem;
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.FeedViewHolder>
private static List<RssItem> items = null;
private static Context context;
public RVAdapter(Context context, List<RssItem> items)
this.items = items;
this.context = context;
public static List<RssItem> getItems()
return items;
@Override
public RVAdapter.FeedViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
return new FeedViewHolder(v);
@Override
public void onBindViewHolder(RVAdapter.FeedViewHolder holder, int position)
FeedViewHolder.getTitle().setText(items.get(position).getTitle());
FeedViewHolder.getPubDate().setText(items.get(position).getPubDate());
//FeedViewHolder.getDescription().setText(items.get(position).getDescription());
@Override
public long getItemId(int id)
return id;
@Override
public int getItemCount()
return items.size();
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView)
super.onAttachedToRecyclerView(recyclerView);
public static class FeedViewHolder extends RecyclerView.ViewHolder
private static CardView cv;
private static TextView title;
private static TextView pubDate;
private static TextView description;
FeedViewHolder(View itemView)
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
title = (TextView) itemView.findViewById(R.id.title);
pubDate = (TextView) itemView.findViewById(R.id.pubDate);
//description = (TextView) itemView.findViewById(R.id.description);
public static TextView getTitle()
return title;
public static TextView getPubDate()
return pubDate;
public static TextView getDescription()
return description;
I believe maybe some works have to be done with the recycling process of RecyclerView but nothing i tried seesm to work.
android android-recyclerview
android android-recyclerview
edited Sep 25 '15 at 2:59
Meenesh Jain
2,48221429
2,48221429
asked Sep 24 '15 at 21:34
James Nguyen
91115
91115
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
up vote
25
down vote
Only two things in your RecyclerView adapter
@Override
public long getItemId(int position)
return position;
and in constructor of adapter
setHasStableIds(true);
Hope it helps!
add a comment |
up vote
25
down vote
done .
@Override
public long getItemId(int position)
return position;
@Override
public int getItemViewType(int position)
return position;
add a comment |
up vote
9
down vote
Override these methods in your Adapter class like this.
@Override
public long getItemId(int position)
return position;
@Override
public int getItemViewType(int position)
return position;
thanks @rjdkolb
– Ajeet yadav
Sep 16 '17 at 7:01
add a comment |
up vote
5
down vote
The problem is the CardView
and TextView
objects are declared static inside the FeedViewHolder
. That means that all the calls trying to set the title in the onBindViewHolder
method hit the latest inflated View.
The fix is to remove the static
from cv
, title
, pubDate
, description
, then implement some non static setters like:
public void setTitle(String s)
title.setText(s);
To be called in the onBindViewHolder
method:
@Override
public void onBindViewHolder(RVAdapter.FeedViewHolder holder, int position)
holder.setTitle(items.get(position).getTitle());
//...
This worked for me. The views had to be initialized as objects of ViewHolder class and not the Adapter class. Then they had to be called from the reference of the holder object in onBindViewHolder
– Aman Alam
Jun 6 '17 at 18:35
add a comment |
up vote
0
down vote
In kotlin with clearing the previous items from the LinearLayout list item from the RecyclerView
override fun onBindViewHolder(view: MyViewHolder, index: Int)
view.guideLinearLayout.removeAllViews()
/* do binding here*/
override fun getItemId(position: Int): Long
return position.toLong()
override fun getItemViewType(position: Int): Int
return position
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32771302%2frecyclerview-items-duplicate-and-constantly-changing%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
25
down vote
Only two things in your RecyclerView adapter
@Override
public long getItemId(int position)
return position;
and in constructor of adapter
setHasStableIds(true);
Hope it helps!
add a comment |
up vote
25
down vote
Only two things in your RecyclerView adapter
@Override
public long getItemId(int position)
return position;
and in constructor of adapter
setHasStableIds(true);
Hope it helps!
add a comment |
up vote
25
down vote
up vote
25
down vote
Only two things in your RecyclerView adapter
@Override
public long getItemId(int position)
return position;
and in constructor of adapter
setHasStableIds(true);
Hope it helps!
Only two things in your RecyclerView adapter
@Override
public long getItemId(int position)
return position;
and in constructor of adapter
setHasStableIds(true);
Hope it helps!
answered Jul 1 '16 at 6:10
Dhrupal
1,24711730
1,24711730
add a comment |
add a comment |
up vote
25
down vote
done .
@Override
public long getItemId(int position)
return position;
@Override
public int getItemViewType(int position)
return position;
add a comment |
up vote
25
down vote
done .
@Override
public long getItemId(int position)
return position;
@Override
public int getItemViewType(int position)
return position;
add a comment |
up vote
25
down vote
up vote
25
down vote
done .
@Override
public long getItemId(int position)
return position;
@Override
public int getItemViewType(int position)
return position;
done .
@Override
public long getItemId(int position)
return position;
@Override
public int getItemViewType(int position)
return position;
answered Dec 16 '16 at 20:16
Sujit Yadav
1,1141916
1,1141916
add a comment |
add a comment |
up vote
9
down vote
Override these methods in your Adapter class like this.
@Override
public long getItemId(int position)
return position;
@Override
public int getItemViewType(int position)
return position;
thanks @rjdkolb
– Ajeet yadav
Sep 16 '17 at 7:01
add a comment |
up vote
9
down vote
Override these methods in your Adapter class like this.
@Override
public long getItemId(int position)
return position;
@Override
public int getItemViewType(int position)
return position;
thanks @rjdkolb
– Ajeet yadav
Sep 16 '17 at 7:01
add a comment |
up vote
9
down vote
up vote
9
down vote
Override these methods in your Adapter class like this.
@Override
public long getItemId(int position)
return position;
@Override
public int getItemViewType(int position)
return position;
Override these methods in your Adapter class like this.
@Override
public long getItemId(int position)
return position;
@Override
public int getItemViewType(int position)
return position;
edited Aug 4 '17 at 19:10
user6655984
answered Aug 4 '17 at 9:36
Ajeet yadav
189113
189113
thanks @rjdkolb
– Ajeet yadav
Sep 16 '17 at 7:01
add a comment |
thanks @rjdkolb
– Ajeet yadav
Sep 16 '17 at 7:01
thanks @rjdkolb
– Ajeet yadav
Sep 16 '17 at 7:01
thanks @rjdkolb
– Ajeet yadav
Sep 16 '17 at 7:01
add a comment |
up vote
5
down vote
The problem is the CardView
and TextView
objects are declared static inside the FeedViewHolder
. That means that all the calls trying to set the title in the onBindViewHolder
method hit the latest inflated View.
The fix is to remove the static
from cv
, title
, pubDate
, description
, then implement some non static setters like:
public void setTitle(String s)
title.setText(s);
To be called in the onBindViewHolder
method:
@Override
public void onBindViewHolder(RVAdapter.FeedViewHolder holder, int position)
holder.setTitle(items.get(position).getTitle());
//...
This worked for me. The views had to be initialized as objects of ViewHolder class and not the Adapter class. Then they had to be called from the reference of the holder object in onBindViewHolder
– Aman Alam
Jun 6 '17 at 18:35
add a comment |
up vote
5
down vote
The problem is the CardView
and TextView
objects are declared static inside the FeedViewHolder
. That means that all the calls trying to set the title in the onBindViewHolder
method hit the latest inflated View.
The fix is to remove the static
from cv
, title
, pubDate
, description
, then implement some non static setters like:
public void setTitle(String s)
title.setText(s);
To be called in the onBindViewHolder
method:
@Override
public void onBindViewHolder(RVAdapter.FeedViewHolder holder, int position)
holder.setTitle(items.get(position).getTitle());
//...
This worked for me. The views had to be initialized as objects of ViewHolder class and not the Adapter class. Then they had to be called from the reference of the holder object in onBindViewHolder
– Aman Alam
Jun 6 '17 at 18:35
add a comment |
up vote
5
down vote
up vote
5
down vote
The problem is the CardView
and TextView
objects are declared static inside the FeedViewHolder
. That means that all the calls trying to set the title in the onBindViewHolder
method hit the latest inflated View.
The fix is to remove the static
from cv
, title
, pubDate
, description
, then implement some non static setters like:
public void setTitle(String s)
title.setText(s);
To be called in the onBindViewHolder
method:
@Override
public void onBindViewHolder(RVAdapter.FeedViewHolder holder, int position)
holder.setTitle(items.get(position).getTitle());
//...
The problem is the CardView
and TextView
objects are declared static inside the FeedViewHolder
. That means that all the calls trying to set the title in the onBindViewHolder
method hit the latest inflated View.
The fix is to remove the static
from cv
, title
, pubDate
, description
, then implement some non static setters like:
public void setTitle(String s)
title.setText(s);
To be called in the onBindViewHolder
method:
@Override
public void onBindViewHolder(RVAdapter.FeedViewHolder holder, int position)
holder.setTitle(items.get(position).getTitle());
//...
answered Sep 24 '15 at 21:55
ulcica
299211
299211
This worked for me. The views had to be initialized as objects of ViewHolder class and not the Adapter class. Then they had to be called from the reference of the holder object in onBindViewHolder
– Aman Alam
Jun 6 '17 at 18:35
add a comment |
This worked for me. The views had to be initialized as objects of ViewHolder class and not the Adapter class. Then they had to be called from the reference of the holder object in onBindViewHolder
– Aman Alam
Jun 6 '17 at 18:35
This worked for me. The views had to be initialized as objects of ViewHolder class and not the Adapter class. Then they had to be called from the reference of the holder object in onBindViewHolder
– Aman Alam
Jun 6 '17 at 18:35
This worked for me. The views had to be initialized as objects of ViewHolder class and not the Adapter class. Then they had to be called from the reference of the holder object in onBindViewHolder
– Aman Alam
Jun 6 '17 at 18:35
add a comment |
up vote
0
down vote
In kotlin with clearing the previous items from the LinearLayout list item from the RecyclerView
override fun onBindViewHolder(view: MyViewHolder, index: Int)
view.guideLinearLayout.removeAllViews()
/* do binding here*/
override fun getItemId(position: Int): Long
return position.toLong()
override fun getItemViewType(position: Int): Int
return position
add a comment |
up vote
0
down vote
In kotlin with clearing the previous items from the LinearLayout list item from the RecyclerView
override fun onBindViewHolder(view: MyViewHolder, index: Int)
view.guideLinearLayout.removeAllViews()
/* do binding here*/
override fun getItemId(position: Int): Long
return position.toLong()
override fun getItemViewType(position: Int): Int
return position
add a comment |
up vote
0
down vote
up vote
0
down vote
In kotlin with clearing the previous items from the LinearLayout list item from the RecyclerView
override fun onBindViewHolder(view: MyViewHolder, index: Int)
view.guideLinearLayout.removeAllViews()
/* do binding here*/
override fun getItemId(position: Int): Long
return position.toLong()
override fun getItemViewType(position: Int): Int
return position
In kotlin with clearing the previous items from the LinearLayout list item from the RecyclerView
override fun onBindViewHolder(view: MyViewHolder, index: Int)
view.guideLinearLayout.removeAllViews()
/* do binding here*/
override fun getItemId(position: Int): Long
return position.toLong()
override fun getItemViewType(position: Int): Int
return position
answered Nov 10 at 18:20
Alex Bartiş
553513
553513
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32771302%2frecyclerview-items-duplicate-and-constantly-changing%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown