Seb Seb - 1 year ago 73
Android Question

ArrayList duplicate element show-up in list view

I'm developing an app (twitter-like) and my feed is based on a listview + adapter. by default you get the 20 first elements of your feeds and when scrolling down and reaching the end of the list, the fragment which contain the view send a request to twitter to get the remaining feeds and as soon as we get them, I update the list handle by the adapter and send the data notification changed. the overall process works but I got a but at the end of the list.

When reaching the end of the list and getting the new list, the last tweets is duplicated which mean that in my feed you have 2 times the same tweets.

For example, you have this feed: tweetA, tweetB, tweetC. When reaching tweetC, we asking for the old tweets and once received, I'm using method called setData to give the new list to the adapter and request the notify data set change. but when you get the list and update, you see the listview showing:

tweetA, tweetB, tweetC, tweetC, tweet D, tweetF

I was thinking it's coming from the new list received and not the listview and thinking that I can just remove the first element of the new list but the issue remain, in my case (when removing the first element), tweet D disappear and show tweetF

here is the code:

Feedfragment :

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "inside onCreateView");

View view = inflater.inflate(R.layout.twitter_swip_list,container, false);
mlistview = (ListView) view.findViewById( ;
TwitterTimeLines twitt = new TwitterTimeLines(mActivity,TwitterHomeActivity.getTwitterHandler());
twitt.getTimeline(handler); return view;

handler is used to get back the result from twitter

private final Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
ArrayList<Status> status = (ArrayList<Status>) msg.obj ;
mStatus = status;
mTwitterFeedAdapter.setData (mStatus);
} ;

Adapter :

public View getView(final int position, View convertView, ViewGroup parent) {
Log.d(TAG,"position"+position) ;
mPosition = position;

mViewHolder = new TweetViewHolder(mActivity);

mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;

if(convertView == null) {
convertView = mInflater.inflate(R.layout.twitter_feed,null) ;
mViewHolder.userProfilePicture = (ImageView) convertView.findViewById( ;
mViewHolder.userRealName = (TextView) convertView.findViewById( ;
convertView.setTag(mViewHolder) ;
else {
mViewHolder = (TweetViewHolder) convertView.getTag() ;
/* detect end of the list and request old tweets*/
if(position >= mStatus.size() - 1 ){

/* both classes are used to extract data and put it in a easy
user format */
mTweetMediaInfo = new TweetMediaInfo(mStatus.get(position));
mTweetInfo = new TweetInfo(mStatus.get(position));


return convertView ;


public void setData(ArrayList<Status> status){
mStatus = status;

Any idea ?

Answer Source

you can use HasHSet like this for removing the redudancey

list = new ArrayList<String>(new LinkedHashSet<String>(list))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download