此链接上的代码运行良好,直到在getCount()中返回的有限数字。但是当我为ex提供大的价值时。 55,在getCount()的返回前面,在23位的模拟器上(即Profile 24)没有显示。在它的位置,显示了Profile ..只是尝试运行代码,你会让我更好。只需在上面的链接代码中进行一次更改,而不是在getCount()类型55中进行5次更改。并运行并查看.. 如果你有任何解决方案..Plzzz让我知道。我希望你帮助我。
答案 0 :(得分:1)
每次框架重用视图(即提供给convertView
方法的getView()
对象不为空)时,您必须找到适当的视图并设置其属性。
在引用链接下的代码示例中,作者仅在创建时为视图设置值,因此每次框架重用视图时,它都具有相同的属性。因此getView()
方法应该如下实现:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView==null){
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.icon, null);
}
else
{
v = convertView;
}
TextView tv = (TextView)v.findViewById(R.id.icon_text);
tv.setText("Profile "+ position);
ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setImageResource(R.drawable.icon);
return v;
}
您可以通过应用视图持有者模式来优化进一步的getView()
方法。
请参阅Romain Guy和Adam Powell的演示文稿The world of ListView,因为它解释了与扩展AdapterView
类的小部件相关的其他一些捕获。