自定义适配器无Id值

时间:2013-05-05 21:51:27

标签: java android

您好我正在尝试从listview填充baseadapter,而在listview上选择时,会传播错误的id

这是我的适配器

public class MyCustomBaseAdapter extends BaseAdapter {
    private static ArrayList<Custom> AList;

    private LayoutInflater inflat;

    public MyCustomBaseAdapter(Context context, ArrayList<Custom> results) {
        AList = results;
        inflat = LayoutInflater.from(context);
    }

    public int getCount() {
        return AList.size();
    }

    public Object getItem(int position) {
        return AList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflat.inflate(R.layout.suspect_list, null);
            holder = new ViewHolder();
            holder.txtName = (TextView) convertView.findViewById(R.id.suspect_name);
            holder.txtSex = (TextView) convertView.findViewById(R.id.suspect_sex);
            holder.txtId = (TextView) convertView.findViewById(R.id.suspect_id);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtName.setText(AList.get(position).getName());
        holder.txtSex.setText(AList.get(position).getSex());
        holder.txtId.setText(AList.get(position).getID());

        return convertView;
    }

    static class ViewHolder {
        TextView txtName;
        TextView txtSex;
        TextView txtId;
    }

}

您可以提供的任何帮助都会很棒 可根据要求提供更多代码

1 个答案:

答案 0 :(得分:0)

你的getItemId()应该返回id。 所以它应该是这样的 - &gt;

public long getItemId(int position) {
        return AList.get(position).getID();
    }

假设ID很长,如果不是那么使用 - &gt; Long.parseLong(AList.get(position).getID());