【问题标题】:ListView Changes Data on ScrollListView 在滚动时更改数据
【发布时间】:2015-12-30 04:48:57
【问题描述】:

我有一个ListView,它在创建时正确地填充了一个数组,但是当我滚动时,数据会发生变化并且完全乱序。我不知道问题出在我的CustomListViewAdapter 还是在我的DetailsPage 中。这是我用来生成textViewsFor Loop

if (currentObject.setTime != null && currentObject.setName != null) {
            String[] temporaryNames = currentObject.setName;
            int totalNames = (currentObject.setTime.length / currentObject.setName.length);

            for (int i = 1; i < totalNames; i++) {
                currentObject.setName = ArrayUtils.addAll(currentObject.setName,temporaryNames);
            }
        }

    listView.setAdapter(new BusRouteCustomListViewAdapter(this, currentObject.setName, currentObject.setTime));

这是我的customListViewAdapter的代码:

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

    Holder holder = new Holder();
    View v = convertView;
    if (v == null)
        if (locations[position] != null && times[position] != null) {
            v = inflater.inflate(R.layout.busdetailrow, null);

            holder.tv1 = (TextView) v.findViewById(R.id.text);
            holder.tv2 = (TextView) v.findViewById(R.id.text2);
            holder.tv1.setText(locations[position]);
            holder.tv2.setText(times[position]);

        } else {

            v = inflater.inflate(R.layout.null_row, null);
        }

    return v;
}

【问题讨论】:

  • ...在下面查看我的答案...如果有问题请告诉我

标签: android listview for-loop android-listview


【解决方案1】:

你用这几行代码替换代码

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

    final Holder holder;
    View v = convertView;
    if (v == null)
        {

           holder = new Holder();
            v = inflater.inflate(R.layout.busdetailrow, null);

            holder.tv1 = (TextView) v.findViewById(R.id.text);
            holder.tv2 = (TextView) v.findViewById(R.id.text2);

        v.setTag(holder);
        } else {

           holder = (Holder) v.getTag();
        }

            holder.tv1.setText(locations[position]);
            holder.tv2.setText(times[position]);

    return v;
 }

【讨论】:

  • 那行得通:) 我想做的是,如果数组中的字符串是null,那么隐藏 listView 行。这就是为什么我有: v = inflater.inflate(R.layout.null_row, null);
  • 我的回答也是。如果使用 Holder,则不要在 convertView == null 时为任何视图设置值
  • @DanielChambers ... Wc 兄弟.. 如果它对你有用.. 请接受其他人的答案,这些人也可能会被困在未来
  • 它不会让我再等 3 分钟。我正在计划它
【解决方案2】:

使用您的 getView(),如下所示。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Holder holder = new Holder();
    if (convertView == null){
        if (locations[position] != null && times[position] != null) {
            convertView = inflater.inflate(R.layout.busdetailrow, null);
            holder.tv1 = (TextView) v.findViewById(R.id.text);
            holder.tv2 = (TextView) v.findViewById(R.id.text2);
            holder.tv1.setText(locations[position]);
            holder.tv2.setText(times[position]);
        } else {

        }
        convertView.setTag(holder);
    }else{
        holder = (Holder) convertView.getTag();
    }
    return convertView;
}

【讨论】:

  • 感谢帮助:)
  • 欢迎。很高兴能帮助你。 :)
【解决方案3】:

你可能没有实现设计持有人。对您的 getView 进行以下更改,然后它将正常工作

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

    if (locations[position] != null && times[position] != null) {
        convertView = inflater.inflate(R.layout.busdetailrow, null);
        holder.tv1 = (TextView) v.findViewById(R.id.text);
        holder.tv2 = (TextView) v.findViewById(R.id.text2);
        holder.tv1.setText(locations[position]);
        holder.tv2.setText(times[position]);

    } 
      else 
    {

    }
    convertView.setTag(holder);
}else{
    holder = (Holder) convertView.getTag();
}
return convertView;
}

【讨论】:

  • 不要复制答案。并且不要在这里发布相同的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多