如何将对象数组与 Android ArrayAdapter 一起使用?
How do I use an array of objects with the Android ArrayAdapter?
我需要使用 ArrayAdapter 来填充我的 Android 应用程序中的 ListView。为了use the 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:
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
- The string array
Then simply call setAdapter() on your ListView:
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
但是,我没有字符串数组,我有一个包含字符串值的对象数组。
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; }
}
我的布局确实包含一个与我的对象中的值相对应的 TextView。
这是我的 ListView 布局:
<LinearLayout android:orientation="vertical">
<ListView android:id="@+id/listOfHeaders" >
</LinearLayout>
这里是要由 ArrayAdapter 填充的 ListView 中每一行的布局:
<LinearLayout android:orientation="horizontal">
<TextView android:id="@+id/toTextView" />
<TextView android:id="@+id/fromTextView" />
<TextView android:id="@+id/subjectTextView" />
</LinearLayout>
对于您的信息,您实际上可以通过覆盖 getView() 方法来实现。基本上你必须为你的 ListView 项目提供自定义布局,并且你必须在你的 getView() 方法中膨胀。欲了解更多信息,您可以 link 这个:http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter
使用 ArrayAdapter 是不够的,您需要扩展 ArrayAdapter 并创建一个自定义适配器,这样您就可以覆盖行创建以使用您的列表布局。请检查此示例:
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;
}
}
对于 Activity,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);
您可以看到 CustomAdapter 使用 HeaderHolder 作为 ViewHolder 模式的一部分,因此列表管理是高效的。
我需要使用 ArrayAdapter 来填充我的 Android 应用程序中的 ListView。为了use the 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:
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
- The string array
Then simply call setAdapter() on your ListView:
ListView listView = (ListView) findViewById(R.id.listview); listView.setAdapter(adapter);
但是,我没有字符串数组,我有一个包含字符串值的对象数组。
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; }
}
我的布局确实包含一个与我的对象中的值相对应的 TextView。
这是我的 ListView 布局:
<LinearLayout android:orientation="vertical">
<ListView android:id="@+id/listOfHeaders" >
</LinearLayout>
这里是要由 ArrayAdapter 填充的 ListView 中每一行的布局:
<LinearLayout android:orientation="horizontal">
<TextView android:id="@+id/toTextView" />
<TextView android:id="@+id/fromTextView" />
<TextView android:id="@+id/subjectTextView" />
</LinearLayout>
对于您的信息,您实际上可以通过覆盖 getView() 方法来实现。基本上你必须为你的 ListView 项目提供自定义布局,并且你必须在你的 getView() 方法中膨胀。欲了解更多信息,您可以 link 这个:http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter
使用 ArrayAdapter 是不够的,您需要扩展 ArrayAdapter 并创建一个自定义适配器,这样您就可以覆盖行创建以使用您的列表布局。请检查此示例:
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;
}
}
对于 Activity,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);
您可以看到 CustomAdapter 使用 HeaderHolder 作为 ViewHolder 模式的一部分,因此列表管理是高效的。