ListView在加载时重复数据更多 [英] ListView repeating the data on load more

查看:96
本文介绍了ListView在加载时重复数据更多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在滚动使用以加载更多数据并在列表视图中显示它们,但我的ListView首先显示已加载的数据,然后将新添加的数据添加到ListView

I am using on scroll to load more data and show them in list view but my ListView first shows already loaded data then adds the newly added data to ListView

我已将和许多其他参考用作参考,但无法解决问题

I have used for reference this and many others but unable to solve the issue

这是我的活动课

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_all);
        ButterKnife.bind(this);

        Users = Users.getCurrentUser(this);

        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        UIUtility.setStatusColor(this);

        Intent intent = getIntent();
        try {
            entityData = new JSONArray(intent.getStringExtra(EXTRA_DATA));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        viewAllCategory = intent.getIntExtra(EXTRA_CATEGORY, 0);
        viewAllEntityType = intent.getIntExtra(EXTRA_TYPE, 0);
        latitude = intent.getDoubleExtra(EXTRA_LATITUDE, 0);
        longitude = intent.getDoubleExtra(EXTRA_LONGITUDE, 0);

        setTitle(TITLES[viewAllCategory]);

        if (viewAllEntityType == Constants.SMBEntityType.TYPE_USER) {
            List<Users> users = Utility.convertJsonArrayToUsers(entityData);
            adapterUser = new ViewAllUserAdapter(this, users);
            lvEntity.setAdapter(adapterUser);
        }   else {
            List<Book> books = Utility.convertJsonArrayToBook(entityData);
            adapterBook = new ViewAllBookAdapter(this, books);
            lvEntity.setAdapter(adapterBook);
        }   
        lvEntity.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (!stopLoadingMore) {
                    if (totalItemCount == firstVisibleItem + visibleItemCount && !isLoading) {  
                        isLoading = true;
                        showLoading();
                        loadMoreData();
                    }
                }
            }
        });
    }

    private void loadMoreData() {
        String loadMoreUrl = buildLoadMoreUrl();
        ShareMyBookHttpClient client = new ShareMyBookHttpClient();
        client.get(loadMoreUrl, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                hideLoading();
                if (responseBody != null) {
                    start = start+20;
                    try {
                        JSONObject jo = new JSONObject(new String(responseBody));
                        if (!jo.getBoolean(Constants.JsonKeys.ERROR)) {
                            JSONArray data = jo.getJSONArray(Constants.JsonKeys.DATA);
                            if (data.length() == 0) {
                                stopLoadingMore = true;
                                return;
                            }
                            if (data.length() < 20) {
                                stopLoadingMore = true;
                            }
                            for (int i = 0; i < data.length(); i++) {
                                entityData.put(data.getJSONObject(i));
                            }
                            if (viewAllEntityType == Constants.SMBEntityType.TYPE_USER) {
                                List<Users> users = Utility.convertJsonArrayToUsers(entityData);
                                adapterUser.mergeArray(users);
                            } else {
                                List<Book> books = Utility.convertJsonArrayToBook(entityData);
                                adapterBook.mergeArray(books);
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                hideLoading();
            }
        });
    }

    private String buildLoadMoreUrl() {
        switch (viewAllCategory) {
            default:
            case ViewAllCategory.CATEGORY_USER:
                return Constants.createUrlToGetViewAllUsers(Users.getId(), latitude, longitude, Users.getAuthToken(), start, LIMIT);

            case ViewAllCategory.CATEGORY_MY_BOOKS:
                return Constants.createUrlToGetViewAllMyBooks(Users.getId(), Users.getAuthToken(), start, LIMIT);
        }
    }

    private void showLoading() {
        sbLoading = Snackbar.make(coolMain, R.string.view_all_loading, Snackbar.LENGTH_INDEFINITE);
        sbLoading.show();
    }

    private void hideLoading() {
        isLoading = false;
        if (sbLoading != null) {
            sbLoading.dismiss();
        }
    }

,以下是我的AdapterClasss

public class ViewAllUserAdapter extends BaseAdapter {

    private List<Users> users;
    private Context context;

    public ViewAllUserAdapter(Context context, List<Users> usersData) {
        this.context = context;
        users = usersData;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row=convertView;
        UserVH holder=null;
        if(row==null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            row= inflater.inflate(R.layout.list_item_view_all_user, parent, false);
            holder = new UserVH(row, position);

            Users user = users.get(position);
            UIUtility.setUserProfileUsingPicasso(context, user.getProfileImageUrlSmall(), holder.ivUserImage);

            holder.tvName.setText(user.getName());
            if (user.getDistance() == -1) {
                holder.tvDistance.setText("");
            } else {
                holder.tvDistance.setText(context.getString(R.string.distance_notation, user.getDistance()));
            }
            row.setTag(holder);
        }
        else {
            holder=(UserVH)row.getTag();Users user = users.get(position);
            UIUtility.setUserProfileUsingPicasso(context, user.getProfileImageUrlSmall(), holder.ivUserImage);

            holder.tvName.setText(user.getName());
            if (user.getDistance() == -1) {
                holder.tvDistance.setText("");
            } else {
                holder.tvDistance.setText(context.getString(R.string.distance_notation, user.getDistance()));
            }

        }

        return row;
    }

    @Override
    public int getCount() {
        return users == null ? 0 : users.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public void mergeArray(List<Users> additionalUsers) {
        Log.e("users", String.valueOf(additionalUsers.size()));
        if (additionalUsers != null) {
            for (int i = 0; i < additionalUsers.size(); i++) {
                if(!(users.contains(additionalUsers.get(i)))) {
                    users.add(additionalUsers.get(i));
                }
            }
        }
        notifyDataSetChanged();
    }

    public class UserVH extends View {
        @Bind(R.id.item_vau_iv_user_image)
        ImageView ivUserImage;
        @Bind(R.id.item_vau_tv_name)
        TextView tvName;
        @Bind(R.id.item_vau_tv_distance)
        TextView tvDistance;

        private int position;

        public UserVH(View view, final int position) {
            super(context);
            this.position = position;
            ButterKnife.bind(this, view);
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    UIUtility.openUserDetailActivity(context, users.get(position));
                }
            });
        }
    }
}

推荐答案

您已经提到链接,您确实发生了同样的问题,您应该在getView方法中对holder类进行正确的实现.

As you already referred this link, Exactly the same issue is happening with you, You should have correct implementation for holder class in getView method.

按照以下说明更新getView()方法:

Update your getView() method as per below :

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row=convertView;
        UserVH holder=null;
        if(row==null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            row= inflater.inflate(R.layout.list_item_view_all_user, parent, false);
            holder = new UserVH(row, position);

            row.setTag(holder);
        }
        else {
            holder=(UserVH)row.getTag();

        }



        Users user = users.get(position);
        UIUtility.setUserProfileUsingPicasso(context, user.getProfileImageUrlSmall(), holder.ivUserImage);

            holder.tvName.setText(user.getName());
            if (user.getDistance() == -1) {
                holder.tvDistance.setText("");
            } else {
                holder.tvDistance.setText(context.getString(R.string.distance_notation, user.getDistance()));
            }

        return row;
    }

并按照以下说明更新getItemId和getItem方法:

And update your getItemId and getItem methods as per below :

@Override
public Object getItem(int position) {
    return users.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

在添加新对象之前清除其他用户列表

EDIT : clear the list additional users before adding the new objects

public void mergeArray(List<Users> additionalUsers) {
    Log.e("users", String.valueOf(additionalUsers.size()));
    if (additionalUsers != null) {
        // Following line to be added          
        additonalusers.clear();
        for (int i = 0; i < additionalUsers.size(); i++) {
            if(!(users.contains(additionalUsers.get(i)))) {
                users.add(additionalUsers.get(i));
            }
        }
    }
    notifyDataSetChanged();
}

这篇关于ListView在加载时重复数据更多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆