为Listview项设置android的标识符

时间:2015-02-25 13:18:33

标签: android android-listview baseadapter

我正在从人员数据库中创建列表视图。当我点击这个人时,我想提出这些人的详细信息。我想为这个人设置我自己的id,然后提出与此id相对应的详细信息。

我尝试通过向标记对象添加int id并在onItemClick()方法中获取views标记来实现此目的...这适用于列表中的前三项(位置0,1,2)但是第四和第五项提出了第0位的详细信息,第六项提出了第1位的详细信息。然而它确实提出了这些项目的正确细节,这似乎很奇怪。

这是我的baseadapter:

public class ItemListBaseAdapter extends BaseAdapter {
    private static ArrayList<PersonObject> itemDetailsrrayList;
    private LayoutInflater l_Inflater;

    public ItemListBaseAdapter(Context context, ArrayList<PersonObject> results) {
        itemDetailsrrayList = results;
        l_Inflater = LayoutInflater.from(context);

    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = l_Inflater.inflate(R.layout.personlist, null);
            holder = new ViewHolder();

            holder.Name = (TextView) convertView.findViewById(R.id.namespace);
            holder.id=itemDetailsrrayList.get(position).getId();
            convertView.setTag(holder);

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


        holder.Name.setText(itemDetailsrrayList.get(position).getName());

        return convertView;
    }
}

这是显示列表视图的片段:

public class HomeFragment extends Fragment {


    private ListView lv1;
    private DatabaseHandler db;
    ArrayList<PersonObject> contacts;
    private ViewHolder myTag;
    private ViewHolder vh;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.item_main, container, false);

       lv1 = (ListView) view.findViewById(R.id.listV_main);
        db = new DatabaseHandler(getActivity());
        contacts=db.getAllPersonItems();


        lv1.setAdapter(new ItemListBaseAdapter(getActivity(), contacts));


        lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                vh=(ViewHolder)view.getTag();
                Intent i=new Intent(getActivity(),TabActivityPersonPage.class);

                i.putExtra("id",Integer.toString(vh.id));

                startActivity(i);


            }
        });
return view;
    }

    }

这是我的ViewHolder类:

公共类ViewHolder {

TextView Name;
TextView ReportedMissing;
ImageView itemImage;
TextView Age;
TextView Contact;
TextView Distance;
int id;

}

我可能会以完全错误的方式接近这个,我&#39;不确定。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

在其他部分之后移动跟随行,

holder.id=itemDetailsrrayList.get(position).getId();

所以getView必须是:

 public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = l_Inflater.inflate(R.layout.personlist, null);
            holder = new ViewHolder();

            holder.Name = (TextView) convertView.findViewById(R.id.namespace);

            convertView.setTag(holder);

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

        holder.id=itemDetailsrrayList.get(position).getId();
        holder.Name.setText(itemDetailsrrayList.get(position).getName());

        return convertView;
    }

convertView == null总是不正确,因此id对象中的holder没有更改,您需要在其他部分之后设置您的值以重新初始化该值