ListView重复对象的自定义适配器

时间:2013-11-10 03:17:30

标签: java android listview android-listview repeat

我有一个自定义适配器来填充ListView,我有一个ArrayList,当我执行程序时,ListView完全填充了来自arraylist的第一个对象。

我在stackoverflow上搜索了一个类似的问题,我没有找到解决方案,有人帮助我

Heres 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;
}

}

1 个答案:

答案 0 :(得分:0)

我不确定你的代码究竟是什么问题,但我知道这太复杂了。尝试这样的事情:

@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;
}