This article mainly explains Arrayadapter's creation method, I divides the arrayadapter into three kinds: simple, the style is rich but the content is simple, the content is rich.
By default, Arrayadapter expects to accept a style file that contains only one textview, and then it displays the received data to ToString (that is, the ToString method that invokes the data object) in TextView.
One, simple.
Each row of such a list has only one line of text.
[Java]View Plaincopy
- Of course, the ListView can also be written in layout, and then Findviewbyid () to get out, so that the following will not need Setcontentview (listview);
- 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);
- Setcontentview (listview);
In the code above, Android. R.layout.simple_expandable_list_item_1 is a style that has been provided on Android, and we can also switch to our own XML. However, it is important to note that this XML file can only have one textview. Even layout can not have. Otherwise error: Arrayadapter requires the resource ID to be a TextView
If there is a online_user_list_item.xml under layout, its contents are as follows:
[XHTML]View Plaincopy
- <TextView xmlns:android="http://schemas.android.com/apk/res/android"
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"
- android:id="@+id/online_user_list_item_textview" >
- </TextView>
Then Android. R.layout.simple_expandable_list_item_1 replaced with R.layout.online_user_list_item.
If we want to use a more complex layout, not just one textview, use the following.
Two, the style is rich but the content is simple.
The Online_user_list_item.xml content under Layout is as follows:
[XHTML]View Plaincopy
- <? 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>
The TextView is where we want to show the content. So when building arrayadapter, you should write this:
[Java]View Plaincopy
- arrayadapter<string> adapter = New Arrayadapter<string> (This, R.layout.online_user_list_item, R.id.online_user_list_item_textview);
What if what we need to show is a single textview that can't be loaded and need other components? We can customize it.
Three, rich content (custom arrayadapter).
This requires writing a class that inherits from Arrayadapter and overrides the GetView method. On the code:
[Java]View Plaincopy
- 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;
- }
- }
That's what the activity says.
[Java]View Plaincopy
- list<user> users = new arraylist<user> ();
- User user = new user ();
- User.setaskednum (8);
- User.setlastmsg ("Hello");
- User.setusername ("Pxx");
- Users.add (user);
- Users.add (user);
- Users.add (user);
- Userlistadapter adapter = New Userlistadapter (this,r.layout.online_user_list_item,users);
- Listview.setadapter (adapter);
Ok! It's almost there.
Android Arrayadapter Detailed