<strong>今天和一群初入茅庐的开发者聊天,以为发了如下代码,说只显示一条数据。</strong>
</pre><pre name="code" class="java">public View getView(int position, View convertView, ViewGroup parent) {
helper help = null;
if (convertView == null) {
help = new helper();
convertView = LayoutInflater.from(con).inflate(
R.layout.layout_style, null);
help.name = (TextView) convertView.findViewById(R.id.style_name);
help.score = (TextView) convertView.findViewById(R.id.style_score);
convertView.setTag(help);
}else{
help=(helper) convertView.getTag();
help.name.setText(list.get(position).getName());
help.score.setText(list.get(position).getScore());
System.out.println("convertview:"+position);
}
return convertView;
}
原因是getview每执行一次help帮助类都赋空所致。
这样帮助类就有数据了。低级错误往往让人绞尽脑汁。
<pre name="code" class="java">public View getView(int position, View convertView, ViewGroup parent) {
helper help = null;
if (convertView == null) {
help = new helper();
convertView = LayoutInflater.from(con).inflate(
R.layout.layout_style, null);
help.name = (TextView) convertView.findViewById(R.id.style_name);
help.score = (TextView) convertView.findViewById(R.id.style_score);
convertView.setTag(help);
}else{
help=(helper) convertView.getTag();
}
help.name.setText(list.get(position).getName());
help.score.setText(list.get(position).getScore());
return convertView;
}