[英]Custom adapter for ListView repeating objects
i've a custom adapter to populate an ListView, i have an ArrayList, when i execute the program the ListView is entire populated with the first object from arraylist. 我有一个自定义适配器来填充ListView,我有一个ArrayList,当我执行程序时,ListView会被arraylist中的第一个对象完全填充。
I searched for an similar question here on stackoverflow and i didnt find a solution, plase, someone help me 我在这里搜索了关于stackoverflow的类似问题,但我没有找到解决方案,请有人帮我
Heres playerAdapter.java 继承人playerAdapter.java
public class playerAdapter extends ArrayAdapter<Player>{
private ArrayList<Player> entries;
private Activity activity;
public playerAdapter(Activity a, int textViewResourceId, ArrayList<Player> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
}
public static class ViewHolder{
public TextView item1;
public TextView item2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.itemplayer, null);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.nomecharlist);
//holder.item2 = (TextView) v.findViewById(R.id.small);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final Player custom = entries.get(position);
if (custom != null) {
holder.item1.setText(custom.getNomePlayer());
//holder.item2.setText(custom.getSecond());
}
return v;
}
} }
I'm not sure what the exact problem is with your code, but I do know that's it's much too complicated. 我不确定您的代码到底有什么问题,但是我知道那太复杂了。 Try something like this: 尝试这样的事情:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.itemplayer, null);
}
final Player custom = entries.get(position);
if (custom != null)
((TextView) convertView.findViewById(R.id.nomecharlist)).setText(custom.getNomePlayer());
return convertView;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.