Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 :-).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
154 views
Welcome To Ask or Share your Answers For Others

1 Answer

// 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;
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...