Android交换机中的ListView位置

时间:2011-03-15 19:26:23

标签: android listview

我正在创建一个在listview上显示电视节目的Android应用。我的节目出现在ListView中,但是它们会切换一个卷轴。

它们被创建为costum listview,类在这里:

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

有人可以提供帮助吗?

//卡斯帕

2 个答案:

答案 0 :(得分:1)

在使用回收的视图时,您似乎尝试实施ViewHolder来保存额外的findViewById次调用。问题是您是按照适配器而不是每个回收的视图存储这些视图。您需要创建一个单独的ViewHolder对象,并将其与每个视图一起存储。视图的标签是一个很好的地方。

试试这个:

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

答案 1 :(得分:0)

移动

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

if(convertView == null)区块之外。

视图已被回收,但您只能在创建视图时设置它们。