我需要使用ArrayAdapter在我的Android应用程序中填充ListView。为了use the ArrayAdapter,它说
例如,如果要在ListView中显示一个字符串数组,请使用构造函数初始化一个新的ArrayAdapter,以指定每个字符串和字符串数组的布局:
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,myStringArray);
此构造函数的参数是:
- 您的应用上下文
- 包含数组中每个字符串的TextView的布局
- 字符串数组
然后只需在ListView上调用setAdapter():
ListView listView =(ListView)findViewById(R.id.listview); listView.setAdapter(适配器);
但是,我没有字符串数组,我有一个包含字符串值的对象数组。
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>
以下是ListAdapter填充的ListView中每一行的布局:
<LinearLayout android:orientation="horizontal">
<TextView android:id="@+id/toTextView" />
<TextView android:id="@+id/fromTextView" />
<TextView android:id="@+id/subjectTextView" />
</LinearLayout>
答案 0 :(得分:6)
使用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模式的一部分,因此列表管理非常有效。
答案 1 :(得分:0)
对于您的信息,您实际上可以通过覆盖getView()方法来实现。基本上,您必须为ListView项提供自定义布局,并且必须在getView()方法中进行扩充。有关详细信息,您可以链接到这个:http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter