简体   繁体   English

如何在android中创建对象的listview并通过单击listitems访问对象字段?

[英]how to make a listview of objects in android and access the object fields by clicking listitems?

I have a listview of objects in my android application I have made it by using my custom ArrayAdapter and I want to get the fields of any object that now is a listItem by clicking the listItem but I haven't any idea to do this 我在我的android应用程序中有一个对象的listview ,是通过使用自定义ArrayAdapter ,我想通过单击listItem来获取现在是listItem的任何对象的字段,但是我不知道要这样做

my Content class is: 我的Content类是:

public class Content {
    public String title;
    public String text;
    public int id;
    public Date date;
}

and my ContentAdapter class: 和我的ContentAdapter类:

public class ContentAdapter extends ArrayAdapter<Content> {

    private ArrayList<Content> objects;

    public ContentAdapter(Context context, int textViewResourceId,
            ArrayList<Content> objects) {
        super(context, textViewResourceId, objects);
        this.objects = objects;
    }

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

        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.content_list_item, null);
        }

        Content i = objects.get(position);

        if (i != null) {

            TextView tt = (TextView) v.findViewById(R.id.toptext);
            TextView ttd = (TextView) v.findViewById(R.id.toptextdata);
            TextView mt = (TextView) v.findViewById(R.id.middletext);
            TextView mtd = (TextView) v.findViewById(R.id.middletextdata);

            if (tt != null) {
                tt.setText("title");
            }
            if (ttd != null) {
                ttd.setText(i.title);
            }
            if (mt != null) {
                mt.setText("text:");
            }
            if (mtd != null) {
                mtd.setText(i.text);
            }
        }

        return v;

    }

}

now I want to get date and id by clicking a list item but not show them in the list view 现在,我想通过单击列表项来获取日期和ID,但不在列表视图中显示它们

should I add id and date fields to my custom arrayAdapter class to do this? 我应该在我的自定义arrayAdapter类中添加id和date字段来执行此操作吗?

Assuming that your custom adapter holds a list of Content object, you will have to add an OnItemClickListener to your list view as below and obtain the object clicked and retrieve the attributes. 假设您的自定义适配器包含一个Content对象的列表,则必须将OnItemClickListener添加到列表视图中,如下所示,并获得单击的对象并检索属性。

  listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> adapterView, View view,
                        int position, long arg3) {
                    Content  content = (Content ) adapterView
                            .getItemAtPosition(position);
                    //from the content object retrieve the attributes you require.
                }

            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM