Class Overview
By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.
Constructor
ArrayAdapter(Context context, int resource)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId)
Constructor
ArrayAdapter(Context context, int resource, T[] objects)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
Constructor
ArrayAdapter(Context context, int resource, List<T> objects)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)
第一种用法:
ListView listview = new ListView(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1);
adapter.add("string1");
adapter.add("haha");
adapter.add("heihei");
listview.setAdapter(adapter);
第二种用法:
layout下的online_user_list_item.xml内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/online_user_list_item_textview" android:text="TextView">
</TextView>
<Button
android:text="button"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.online_user_list_item, R.id.online_user_list_item_textview);
第三种用法:
如果我们需要展示的内容是一仅一个textview承载不了的,我们可以自定义
public class UserListAdapter extends ArrayAdapter<User> {
private int resourceId;
public UserListAdapter(Context context, int textViewResourceId, List<User> objects) {
super(context, textViewResourceId, objects);
this.resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
User user = getItem(position);
LinearLayout userListItem = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resourceId, userListItem, true);
TextView tvUsername = (TextView)userListItem.findViewById(R.id.tv_user_list_username);
TextView tvAskedNum = (TextView)userListItem.findViewById(R.id.tv_user_list_askednum);
TextView tvLastMsg = (TextView)userListItem.findViewById(R.id.tv_user_list_lastmsg);
tvUsername.setText(user.getUsername());
tvAskedNum.setText(String.valueOf(user.getAskedNum()));
tvLastMsg.setText(user.getLastMsg());
return userListItem;
}
}
By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.
Constructor
ArrayAdapter(Context context, int resource)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId)
Constructor
ArrayAdapter(Context context, int resource, T[] objects)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
Constructor
ArrayAdapter(Context context, int resource, List<T> objects)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)
第一种用法:
ListView listview = new ListView(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1);
adapter.add("string1");
adapter.add("haha");
adapter.add("heihei");
listview.setAdapter(adapter);
第二种用法:
layout下的online_user_list_item.xml内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/online_user_list_item_textview" android:text="TextView">
</TextView>
<Button
android:text="button"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.online_user_list_item, R.id.online_user_list_item_textview);
第三种用法:
如果我们需要展示的内容是一仅一个textview承载不了的,我们可以自定义
public class UserListAdapter extends ArrayAdapter<User> {
private int resourceId;
public UserListAdapter(Context context, int textViewResourceId, List<User> objects) {
super(context, textViewResourceId, objects);
this.resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
User user = getItem(position);
LinearLayout userListItem = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resourceId, userListItem, true);
TextView tvUsername = (TextView)userListItem.findViewById(R.id.tv_user_list_username);
TextView tvAskedNum = (TextView)userListItem.findViewById(R.id.tv_user_list_askednum);
TextView tvLastMsg = (TextView)userListItem.findViewById(R.id.tv_user_list_lastmsg);
tvUsername.setText(user.getUsername());
tvAskedNum.setText(String.valueOf(user.getAskedNum()));
tvLastMsg.setText(user.getLastMsg());
return userListItem;
}
}