ListView - 自定义适配器 - 滚动时值不断变化

时间:2013-03-17 06:32:15

标签: android android-listview custom-adapter

Android 2.3.3

我正在使用自定义适配器来填充列表视图。列表视图中的每一行都有3个文本视图(自定义布局),这些视图是从数据库中检索的数据中填充的。

问题是,第一次正确显示数据。但是当我向下滚动然后再次向上滚动列表视图时,列表视图顶部的行将被底部的行替换,当我向下滚动时,它们将再次被当前显示的行替换。 / p>

我认为问题可能是重新创建行,我应该使用viewHolder。尽管我确实使用了viewHolder,但它并没有解决问题。

这是代码......

class MyCustomAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return c.getCount();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        HistoryViewHolder viewHolder;

            if(convertView == null)
            {
                LayoutInflater li = getLayoutInflater();
                convertView =  li.inflate(R.layout.history_row, null);

                viewHolder = new HistoryViewHolder();

                viewHolder.txtcnt = (TextView) convertView.findViewById(R.id.txtView_History_Count);
                viewHolder.txtexp = (TextView) convertView.findViewById(R.id.txtView_History_Expression);
                viewHolder.txtres = (TextView) convertView.findViewById(R.id.txtView_History_Result);

                convertView.setTag(viewHolder);
            }
            else
            {
                viewHolder = (HistoryViewHolder) convertView.getTag();
            }


            if(!c.isAfterLast())
            {

                viewHolder.txtcnt.setText(String.valueOf(position + 1));        
                System.out.println("Position"+viewHolder.txtcnt.getText().toString());

                expression = c.getString(c.getColumnIndex("expression"));
                result = c.getString(c.getColumnIndex("result"));

                c.moveToPosition(position + 1);

                viewHolder.txtexp.setText(expression);
                System.out.println("Expression"+viewHolder.txtexp.getText().toString());
                viewHolder.txtres.setText(result);      
                System.out.println("Result"+viewHolder.txtres.getText().toString());

            }   

        return convertView;
    }


    public class HistoryViewHolder {
        TextView txtcnt;
        TextView txtexp;
        TextView txtres;
    }

}

我正确使用viewHolder吗?为什么我们需要使用viewHolder?有什么用?

编辑:::正如vmironov所建议:::我删除了if(!c.isAfterLast())并将c.moveToPosition(position + 1)更改为c.moveToPosition(position)。现在,替换不会发生,但第一个元素重复两次,最后一个条目没有显示。当我再次向下滚动时,前两行没有显示。

编辑2 :::刚刚移动c.moveToPosition(position)以上表达式= c.getString(c.getColumnIndex("expression"));

我发布了整个代码,如果需要看一下......

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    HistoryViewHolder viewHolder;

        if(convertView == null)
        {
            LayoutInflater li = getLayoutInflater();
            convertView =  li.inflate(R.layout.history_row, null);

            viewHolder = new HistoryViewHolder();

            viewHolder.txtcnt = (TextView) convertView.findViewById(R.id.txtView_History_Count);
            viewHolder.txtexp = (TextView) convertView.findViewById(R.id.txtView_History_Expression);
            viewHolder.txtres = (TextView) convertView.findViewById(R.id.txtView_History_Result);

            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (HistoryViewHolder) convertView.getTag();
        }

            viewHolder.txtcnt.setText(String.valueOf(position + 1));        
            System.out.println("Position"+viewHolder.txtcnt.getText().toString());

            c.moveToPosition(position);

            expression = c.getString(c.getColumnIndex("expression"));
            result = c.getString(c.getColumnIndex("result"));

            viewHolder.txtexp.setText(expression);
            System.out.println("Expression"+viewHolder.txtexp.getText().toString());
            viewHolder.txtres.setText(result);      
            System.out.println("Result"+viewHolder.txtres.getText().toString());            


    return convertView;
}


public class HistoryViewHolder {
    TextView txtcnt;
    TextView txtexp;
    TextView txtres;
}

}

1 个答案:

答案 0 :(得分:0)

  1. 删除if (!c.isAfterLast())条件。
  2. c.moveToPosition(position + 1)替换为c.moveToPosition(position)
  3. 必须在c.moveToPosition(position) 之前调用
  4. expression = c.getString(c.getColumnIndex("expression"))