安卓 2.3.3
我正在使用自定义适配器来填充列表视图。列表视图中的每一行都有 3 个文本视图(自定义布局),它们是从从数据库中检索到的数据中填充的。
问题是,数据第一次正确显示。但是当我向下滚动然后再次向上滚动列表视图时,列表视图顶部的行被底部的行替换,当我向下滚动时,它们再次被当前正在显示的行替换。
我认为问题可能是重新创建行,我应该使用 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;
}
}