没有显示正确的文本列表项 [英] List item not showing correct text

查看:136
本文介绍了没有显示正确的文本列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个的ListView 取值可以称他们为1和2。我将项目添加到的ListView 1 然后经过一定的时间量,我谨的项目的ListView 2

我遇到的问题是,有时(我还没有制定出来呢模式)中显示的文本的ListView 1 是错的 - 它显示了previously创建的项目的文本。但是,当我将它移动到的ListView 2 它显示正确的文本。

我理解的观点被回收,以便列表项可以使用旧的观点。我不认为这是这样做的,因为它显示了在其他列表中正确的信息。

这可能是因为它花费的时间太长,设置文本中有 getView()的方法有很多,所以它使用,而旧的价值观念它更新在后台。我不知道这是可能的,但即使我称之为 notifyDataSetChanged()仍在显示了错误的信息。

下面是我的code:

的ListView 1

这是对列表中的适配器:

 公共类PendingAdapter延伸BaseAdapter {            私人列表<地图<弦乐,对象>> mPendingItemList;            公共PendingAdapter(){
                mPendingItemList = DataModel.getInstance()getPendingItemList()。
            }            @覆盖
            公众诠释的getCount(){
                返回mPendingItemList.size();
            }            @覆盖
            公共对象的getItem(INT位置){
                返回mPendingItemList.get(位置);
            }            @覆盖
            众长getItemId(INT位置){
                返回的位置;
            }            @覆盖
            公共查看getView(INT位置,查看convertView,父母的ViewGroup){                如果(空== convertView){                    convertView = LayoutInflater.from(getActivity())。膨胀(
                            R.layout.pending_item,NULL);                }
                TextView的tv_title =(TextView中)convertView
                        .findViewById(R.id.pi_tv_title);
                TextView的tv_content =(TextView中)convertView
                        .findViewById(R.id.pi_tv_content);
                TextView的tv_counter =(TextView中)convertView
                        .findViewById(R.id.pi_tv_counter);
                TextView的tv_ongoing =(TextView中)convertView
                        .findViewById(R.id.pi_tv_ongoing);
                TextView的tv_type =(TextView中)convertView
                        .findViewById(R.id.pi_tv_type);
                TextView的tv_date =(TextView中)convertView
                        .findViewById(R.id.pi_tv_date);                @燮pressWarnings(未登记)
                HashMap的<字符串,字符串> itemDataHashMap =(HashMap的<字符串,字符串>)的getItem(位置);                tv_title.setText(itemDataHashMap.get(行星));
                tv_content.setText(itemDataHashMap.get(内容));
                tv_counter.setText(itemDataHashMap.get(反));
                tv_type.setText(itemDataHashMap.get(类型));
                tv_ongoing.setText(itemDataHashMap.get(持续));
                tv_date.setText(itemDataHashMap.get(DATE));                返回convertView;
            }
        }

我不知道哪个code的其他部分将是有益的(也许 mPendingItemList ) - 但如果更加有助于请说: - )


解决方案

  //试试这个
公共类PendingAdapter延伸BaseAdapter {        私人列表<地图<弦乐,对象>> mPendingItemList;        公共PendingAdapter(){
            mPendingItemList = DataModel.getInstance()getPendingItemList()。
        }        @覆盖
        公众诠释的getCount(){
            返回mPendingItemList.size();
        }        @覆盖
        公共对象的getItem(INT位置){
            返回mPendingItemList.get(位置);
        }        @覆盖
        众长getItemId(INT位置){
            返回的位置;
        }        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
            ViewHolder持有人;
            如果(空== convertView){                convertView = LayoutInflater.from(getActivity())。膨胀(
                        R.layout.pending_item,NULL);
                持有人=新ViewHolder();                holder.tv_title =(TextView中)convertView
                        .findViewById(R.id.pi_tv_title);
                holder.tv_content =(TextView中)convertView
                        .findViewById(R.id.pi_tv_content);
                holder.tv_counter =(TextView中)convertView
                        .findViewById(R.id.pi_tv_counter);
                holder.tv_ongoing =(TextView中)convertView
                        .findViewById(R.id.pi_tv_ongoing);
                holder.tv_type =(TextView中)convertView
                        .findViewById(R.id.pi_tv_type);
                holder.tv_date =(TextView中)convertView
                        .findViewById(R.id.pi_tv_date);
                convertView.setTag(保持器);
            }其他{
                支架=(ViewHolder)convertView.getTag();
            }            @燮pressWarnings(未登记)
            HashMap的<字符串,字符串> itemDataHashMap =(HashMap的<字符串,字符串>)的getItem(位置);            holder.tv_title.setText(itemDataHashMap.get(行星));
            holder.tv_content.setText(itemDataHashMap.get(内容));
            holder.tv_counter.setText(itemDataHashMap.get(反));
            持有人。 tv_type.setText(itemDataHashMap.get(类型));
            holder.tv_ongoing.setText(itemDataHashMap.get(持续));
            holder.tv_date.setText(itemDataHashMap.get(DATE));            convertView.setTag(保持器);
            返回convertView;
        }        静态类ViewHolder {
            TextView的tv_title;
            TextView的tv_content;
            TextView的tv_counter;
            TextView的tv_ongoing;
            TextView的tv_type;
            TextView的tv_date;
        }
    }

I have two ListViews lets call them 1 and 2. I add items to ListView 1 and then after a certain amount of time, I move the item to ListView 2.

The problem I'm having is that sometimes (I haven't worked out the pattern yet) the text shown in ListView 1 is wrong - it shows the previously created item's text. But when I move it to ListView 2 it shows the correct text.

I understand views are recycled so the list item could be using the old view. I don't think it is doing this as it shows the correct info in the other list.

It could be that it is taking too long to set the text as there is a lot of it in the getView() method, so it uses the old values while it updates in the background. I don't know if this is possible, but even when I call notifyDataSetChanged() the wrong info is still being shown.

Here is my code:

ListView 1.

This is the adapter for the List:

  public class PendingAdapter extends BaseAdapter { 

            private List<Map<String, Object>> mPendingItemList;

            public PendingAdapter() {
                mPendingItemList = DataModel.getInstance().getPendingItemList();
            }

            @Override
            public int getCount() {
                return mPendingItemList.size();
            }

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

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

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                if (null == convertView) {

                    convertView = LayoutInflater.from(getActivity()).inflate(
                            R.layout.pending_item, null);

                }


                TextView tv_title = (TextView) convertView
                        .findViewById(R.id.pi_tv_title);
                TextView tv_content = (TextView) convertView
                        .findViewById(R.id.pi_tv_content);
                TextView tv_counter = (TextView) convertView
                        .findViewById(R.id.pi_tv_counter);
                TextView tv_ongoing = (TextView) convertView
                        .findViewById(R.id.pi_tv_ongoing);
                TextView tv_type = (TextView) convertView
                        .findViewById(R.id.pi_tv_type);
                TextView tv_date = (TextView) convertView
                        .findViewById(R.id.pi_tv_date);

                @SuppressWarnings("unchecked")
                HashMap<String, String> itemDataHashMap = (HashMap<String, String>) getItem(position);

                tv_title.setText(itemDataHashMap.get("planet"));
                tv_content.setText(itemDataHashMap.get("content"));
                tv_counter.setText(itemDataHashMap.get("counter"));
                tv_type.setText(itemDataHashMap.get("type"));
                tv_ongoing.setText(itemDataHashMap.get("ongoing"));
                tv_date.setText(itemDataHashMap.get("date"));

                return convertView;
            }
        }

I am not sure which other pieces of code would be helpful (maybe the mPendingItemList) - but if more would help please say :-).

解决方案

// try this one
public class PendingAdapter extends BaseAdapter { 

        private List<Map<String, Object>> mPendingItemList;

        public PendingAdapter() {
            mPendingItemList = DataModel.getInstance().getPendingItemList();
        }

        @Override
        public int getCount() {
            return mPendingItemList.size();
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (null == convertView) {

                convertView = LayoutInflater.from(getActivity()).inflate(
                        R.layout.pending_item, null);
                holder = new ViewHolder();

                holder.tv_title = (TextView) convertView
                        .findViewById(R.id.pi_tv_title);
                holder.tv_content = (TextView) convertView
                        .findViewById(R.id.pi_tv_content);
                holder.tv_counter = (TextView) convertView
                        .findViewById(R.id.pi_tv_counter);
                holder.tv_ongoing = (TextView) convertView
                        .findViewById(R.id.pi_tv_ongoing);
                holder.tv_type = (TextView) convertView
                        .findViewById(R.id.pi_tv_type);
                holder.tv_date = (TextView) convertView
                        .findViewById(R.id.pi_tv_date);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }

            @SuppressWarnings("unchecked")
            HashMap<String, String> itemDataHashMap = (HashMap<String, String>) getItem(position);

            holder.tv_title.setText(itemDataHashMap.get("planet"));
            holder.tv_content.setText(itemDataHashMap.get("content"));
            holder.tv_counter.setText(itemDataHashMap.get("counter"));
            holder. tv_type.setText(itemDataHashMap.get("type"));
            holder.tv_ongoing.setText(itemDataHashMap.get("ongoing"));
            holder.tv_date.setText(itemDataHashMap.get("date"));

            convertView.setTag(holder);
            return convertView;
        }

        static class ViewHolder {
            TextView tv_title;
            TextView tv_content;
            TextView tv_counter;
            TextView tv_ongoing;
            TextView tv_type;
            TextView tv_date;
        }
    }

这篇关于没有显示正确的文本列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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