dchamb
dchamb

Reputation: 179

ListView Changes Data on Scroll

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

Upvotes: 1

Views: 332

Answers (3)

Zeeshan Shabbir
Zeeshan Shabbir

Reputation: 7114

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

Upvotes: 1

Ravindra Kushwaha
Ravindra Kushwaha

Reputation: 8042

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

Upvotes: 2

Chirag Savsani
Chirag Savsani

Reputation: 6140

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

Upvotes: 2

Related Questions