【问题标题】:List item not showing correct text列表项未显示正确的文本
【发布时间】:2013-09-12 01:12:51
【问题描述】:

我有两个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) - 但如果有更多帮助,请说:-)。

【问题讨论】:

    标签: android listview textview


    【解决方案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;
            }
        }
    

    【讨论】:

    • 非常感谢 - 请您解释一下为什么会这样?
    猜你喜欢
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多