BaseAdapter超级白痴的错误,谁能知道原因呢,我认为我没错的

panjianle 发布于 2012/03/08 16:15
阅读 501
收藏 0
getCount的返回的是15,然后在getView中,position 从0~12,然后又从0开始,然后到2的时候就报出没有next()了,其实已经执行了15次,不可能还有next(),但是position为什么会在12时又变回0了,天啊。
class ListViewAdapter extends BaseAdapter{
private Document doc;
private String typeName;
private List<Element> le;
private Iterator lei;
public ListViewAdapter(){
super();
doc = XMLHandler.nativeDoc;
this.typeName="vitamin";
le = doc.getRootElement().getChildren(typeName);
lei =le.iterator();
Log.d("CheckList", "Size is "+le.size());
while(lei.hasNext()){
Element e = (Element)lei.next();
Log.d("CheckList", e.getText());
}
lei = le.iterator();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
Log.d("ListViewAdapter", "Count is "+le.size());
return le.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View v, ViewGroup parent) {
// TODO Auto-generated method stub
final LayoutInflater inflater = LayoutInflater.from(getApplicationContext());  
        if(v == null){  
            v = inflater.inflate(R.layout.listview_layout, null);  
        }
        TextView medichinename = (TextView)v.findViewById(R.id.medichinename);   
        Element e = (Element)lei.next();
        medichinename.setText("药名:" + e.getText());
        Log.d("ListViewAdapter", "Position is "+position+" Text is "+ e.getText());
        return v;
}
  } 
这个是CheckList的LogCat


这个是ListViewAdapter的LogCat,就是这里出错了

加载中
0
迷途d书童
迷途d书童
你在getView方法里,怎么不用Element e = lei.get(position);来获取呢
p
panjianle
用了,但是position确实还会这样,但是用你这个方法比较好,不会出错
0
fneg
fneg
@Override
public View getView(int position, View v, ViewGroup parent) {
// TODO Auto-generated method stub
final LayoutInflater inflater = LayoutInflater.from(getApplicationContext());  
        if(v == null){  
            v = inflater.inflate(R.layout.listview_layout, null);  
        }
        TextView medichinename = (TextView)v.findViewById(R.id.medichinename);   
        Element e = (Element)getItem(position);
        medichinename.setText("药名:" + e.getText());
        Log.d("ListViewAdapter", "Position is "+position+" Text is "+ e.getText());
        return v;
}
  } 
p
panjianle
你的这样只说出了我的错误在哪里,但是没有说是为什么错
0
j
jhq88123.jh

你的一屏显示12行,故有上面的结果,是因为屏幕滚动了……从0开始计算

 

需要得到一个值:StartPosition,然后 StartPosition + Position 即是你要的值;

返回顶部
顶部