0

我有两个ListViews 让我们称它们为 1 和 2。我将项目添加到ListView 1,然后在一定时间后,我将项目移动到ListView 2

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

我了解视图被回收,因此列表项可以使用旧视图。我认为它没有这样做,因为它在另一个列表中显示了正确的信息。

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

这是我的代码:

ListView 1.

这是列表的适配器:

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

我不确定哪些其他代码会有所帮助(也许是mPendingItemList)-但如果有更多帮助,请说:-)。

4

1 回答 1

1
// 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;
        }
    }
于 2013-09-10T04:12:18.167 回答