简体   繁体   English

如何在Android ArrayAdapter中使用对象数组?

[英]How do I use an array of objects with the Android ArrayAdapter?

I need to use an ArrayAdapter to populate a ListView in my Android application. 我需要使用ArrayAdapter填充Android应用程序中的ListView。 In order to use the ArrayAdapter , it says 为了使用ArrayAdapter ,它说

For example, if you have an array of strings you want to display in a ListView, initialize a new ArrayAdapter using a constructor to specify the layout for each string and the string array: 例如,如果您要在ListView中显示一个字符串数组,请使用构造函数初始化一个新的ArrayAdapter,以指定每个字符串和字符串数组的布局:

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, myStringArray); ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,myStringArray);

The arguments for this constructor are: 此构造函数的参数为​​:

  • Your app Context 您的应用上下文
  • The layout that contains a TextView for each string in the array 包含每个数组中的字符串的TextView的布局
  • The string array 字符串数组

Then simply call setAdapter() on your ListView: 然后只需在ListView上调用setAdapter()即可:

ListView listView = (ListView) findViewById(R.id.listview); ListView listView =(ListView)findViewById(R.id.listview); listView.setAdapter(adapter); listView.setAdapter(adapter);

However, I do not have an array of strings, I have an array of objects that contain string values. 但是,我没有字符串数组,但是有包含字符串值的对象数组。

public class Headers {
    private String from;
    private String to;
    private String subject;

    public Headers (String from, String to, String subject){
        this.from = from;
        this.to = to;
        this.subject = subject;
    }

    public String getFrom() { return from; }
    public void setFrom(String from) { this.from = from; }

    public String getTo() { return to; }
    public void setTo(String to) { this.to = to; }

    public String getSubject() { return subject; }
    public void setSubject(String subject) { this.subject = subject; }
}

My layout does contain a TextView corresponding to values in my object. 我的布局确实包含一个与我的对象中的值相对应的TextView。

Here is my layout with my ListView: 这是我的ListView的布局:

<LinearLayout android:orientation="vertical">
    <ListView android:id="@+id/listOfHeaders" >
</LinearLayout>

And here is the layout for each row in the ListView to be populated by the ArrayAdapter: 这是要由ArrayAdapter填充的ListView中每一行的布局:

<LinearLayout android:orientation="horizontal">
    <TextView android:id="@+id/toTextView" />
    <TextView android:id="@+id/fromTextView" />
    <TextView android:id="@+id/subjectTextView" />
</LinearLayout>

Using the ArrayAdapter is not enough, you will need to extend ArrayAdapter and create a custom adapter, so you can overwrite the rows creation to use your list layout. 仅使用ArrayAdapter是不够的,您将需要扩展ArrayAdapter并创建一个自定义适配器,因此您可以覆盖创建的行以使用列表布局。 Please check this example: 请检查以下示例:

public class CustomAdapter extends ArrayAdapter<Headers> {

    Context context;
    int layoutResourceId;
    ArrayList<Headers> data = null;

    public CustomAdapter(Context context, int resource, List<Headers> objects) {
        super(context, resource, objects);
        this.layoutResourceId = resource;
        this.context = context;
        this.data = (ArrayList) objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        HeaderHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new HeaderHolder();
            holder.from = (TextView) row.findViewById(R.id.fromTextView);
            holder.to = (TextView)row.findViewById(R.id.toTextView);
            holder.subject = (TextView)row.findViewById(R.id.subjectTextView);

            row.setTag(holder);
        }
        else
        {
            holder = (HeaderHolder) row.getTag();
        }

        Headers item = data.get(position);
        holder.from.setText(item.getFrom());
        holder.to.setText(item.getTo());
        holder.subject.setText(item.getSubject());

        return row;
    }

    private class HeaderHolder {
        public TextView from;
        public TextView to;
        public TextView subject;

    }
}

For the Activity, on the onCreate method: 对于活动,在onCreate方法上:

ListView list = (ListView) findViewById(R.id.listView);
ArrayList<Headers> data = new ArrayList<Headers>();
data.add(new Headers("from", "to", "subject"));

ArrayAdapter adapter = new CustomAdapter(this, R.layout.list, data);
list.setAdapter(adapter); 

You can see that the CustomAdapter is using HeaderHolder as part of the ViewHolder pattern so the list management is efficient. 您可以看到CustomAdapter将HeaderHolder用作ViewHolder模式的一部分,因此列表管理非常有效。

For your kind of info, you can actually do that by overriding getView() method. 对于您的信息,您实际上可以通过重写getView()方法来实现。 Basically you have to provide custom Layout for your ListView item and that you have to inflate in you getView() method. 基本上,您必须为ListView项提供自定义布局,并且必须在getView()方法中进行充气。 For more info you can link to this one: http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter 有关更多信息,您可以链接到此: http : //www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter

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

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