Android Arrayadapter Detailed

Source: Internet
Author: User

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
  1. 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);
  2. ListView ListView = New ListView (This);
  3. arrayadapter<string> adapter = New Arrayadapter<string> (this,android.  R.layout.simple_expandable_list_item_1);
  4. Adapter.add ("string1");
  5. Adapter.add ("haha");
  6. Adapter.add ("Heihei");
  7. Listview.setadapter (adapter);
  8. 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
    1. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    2. Android:layout_width="Wrap_content"
    3. android:layout_height="Wrap_content"
    4. android:id="@+id/online_user_list_item_textview" >
    5. </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
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content">
  5. <TextView android:layout_width="wrap_content" android:layout_height="Wrap_content " Android:id= "@+id/online_user_list_item_textview" android:text="TextView"></ TextView>
  6. <Button
  7. android:text="button"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content">
  10. </Button>
  11. </linearlayout>

The TextView is where we want to show the content. So when building arrayadapter, you should write this:

[Java]View Plaincopy
    1. 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
  1. Public class Userlistadapter extends arrayadapter<user> {
  2. private int resourceId;
  3. Public Userlistadapter (context context, int Textviewresourceid, list<user> objects) {
  4. Super (context, Textviewresourceid, objects);
  5. This.resourceid = Textviewresourceid;
  6. }
  7. @Override
  8. Public View GetView (int position, View Convertview, ViewGroup parent) {
  9. User user = GetItem (position);
  10. LinearLayout Userlistitem = new LinearLayout (GetContext ());
  11. String inflater = Context.layout_inflater_service;
  12. Layoutinflater VI = (layoutinflater) getcontext (). Getsystemservice (Inflater);
  13. Vi.inflate (ResourceId, Userlistitem, true);
  14. TextView tvusername = (TextView) Userlistitem.findviewbyid (r.id.tv_user_list_username);
  15. TextView tvaskednum = (TextView) Userlistitem.findviewbyid (r.id.tv_user_list_askednum);
  16. TextView tvlastmsg = (TextView) Userlistitem.findviewbyid (r.id.tv_user_list_lastmsg);
  17. Tvusername.settext (User.getusername ());
  18. Tvaskednum.settext (String.valueof (User.getaskednum ()));
  19. Tvlastmsg.settext (User.getlastmsg ());
  20. return userlistitem;
  21. }
  22. }

That's what the activity says.

[Java]View Plaincopy
  1. list<user> users = new arraylist<user> ();
  2. User user = new user ();
  3. User.setaskednum (8);
  4. User.setlastmsg ("Hello");
  5. User.setusername ("Pxx");
  6. Users.add (user);
  7. Users.add (user);
  8. Users.add (user);
  9. Userlistadapter adapter = New Userlistadapter (this,r.layout.online_user_list_item,users);
  10. Listview.setadapter (adapter);

Ok! It's almost there.

Android Arrayadapter Detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.