Casper
Casper

Reputation: 373

ListView in Android switch places

I'm creating an Android app that displays tv-shows on a listview. My shows coming up in the ListView, but they switch places one scrolls.

They are created as costum listview and the class is here:

public class CustomListViewAdapter extends BaseAdapter {
    private static ArrayList<Show> searchArrayList;
    private LayoutInflater mInflater;
    private TextView txtTitle, txtPrev, txtNext;

    public CustomListViewAdapter(Context context, ArrayList<Show> results) {
        searchArrayList = results;
        mInflater = LayoutInflater.from(context);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.custom_row, null);
            txtTitle = (TextView) convertView.findViewById(R.id.title);
            txtPrev = (TextView) convertView.findViewById(R.id.prevNrDate);
            txtNext = (TextView) convertView.findViewById(R.id.nextNrDate);
        }

        txtTitle.setText(searchArrayList.get(position).getTitle());

        txtPrev.setText(searchArrayList.get(position).getPrevNr() + " - " + 
                        searchArrayList.get(position).getPrevDate() + "\n" + 
                        searchArrayList.get(position).getPrevTitle());

        txtNext.setText(searchArrayList.get(position).getNextNr() + " - " + 
                        searchArrayList.get(position).getNextDate() + "\n" + 
                        searchArrayList.get(position).getNextTitle());

        return convertView;
    }
}

Some one who can help?

//Casper

Upvotes: 0

Views: 664

Answers (2)

adamp
adamp

Reputation: 28932

It looks like you were trying to implement a ViewHolder to save the extra findViewById calls when using recycled views. The problem is that you're storing those views per-adapter instead of per-recycled view. You need to create a separate ViewHolder object and store it along with each view. The view's tag is a good place to do this.

Try this:

public class CustomListViewAdapter extends BaseAdapter {
    private static ArrayList<Show> mSearchArrayList;
    private LayoutInflater mInflater;

    public CustomListViewAdapter(Context context, ArrayList<Show> results) {
        mSearchArrayList = results;
        mInflater = LayoutInflater.from(context);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.custom_row, null);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.prev = (TextView) convertView.findViewById(R.id.prevNrDate);
            holder.next = (TextView) convertView.findViewById(R.id.nextNrDate);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Show item = mSearchArrayList.get(position);
        holder.title.setText(item.getTitle());

        holder.prev.setText(item.getPrevNr() + " - " + 
                            item.getPrevDate() + "\n" + 
                            item.getPrevTitle());

        holder.next.setText(item.getNextNr() + " - " + 
                            item.getNextDate() + "\n" + 
                            item.getNextTitle());

        return convertView;
    }

    private static class ViewHolder {
        public TextView title;
        public TextView prev;
        public TextView next;
    }
}

Upvotes: 1

Robby Pond
Robby Pond

Reputation: 73484

Move

txtTitle = (TextView) convertView.findViewById(R.id.title);
txtPrev = (TextView) convertView.findViewById(R.id.prevNrDate);
txtNext = (TextView) convertView.findViewById(R.id.nextNrDate);

outside the if(convertView == null) block.

The views are recycled but you are only setting them when the views get created.

Upvotes: 0

Related Questions