ListView Changes Data on Scroll

痴心易碎 提交于 2020-01-16 01:09:12

问题


I have a ListView that is correctly populated with an array on create, but when I scroll, the data changes and is all out of order. I don't know wether the problem is in my CustomListViewAdapter or in my DetailsPage. Here's the For Loop that I am using to generate the textViews:

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

and here is the code for my 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;
}

回答1:


You replace the code with these lines of code

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



回答2:


Use your getView() like below.

@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:


You didnt implement design holder probably. make following changes to your getView then it will work fine

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


来源:https://stackoverflow.com/questions/34523053/listview-changes-data-on-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!