Android miscellaneous-Use of listview arrayadapter

Source: Internet
Author: User

Arrayadapter

I always feel that I can learn more only when I write my own blog. Although there are a lot of good information on the Internet, it is uneven and it takes a lot of time to find the most desired information, simply write what you need most.

Adapter means the adapter. In Android, listview is widely used, and listview can be used only by combining with various adapters. Different adapters are used in different scenarios, so the most common ones will be helpful to you in the future.

Arrayadapter (array adapter) is generally used to display a line of text information, so it is easier.

Public arrayadapter (context, int textviewresourceid, list <t> objects)

The above line of code is used to assemble data. to assemble the data, an adapter connecting the listview view object and the array data is required to adapt the two. The arrayadapter structure requires three parameters, this is the layout file (note that the layout file here describes the layout of each row in the list. For details, see main. XML file, android. r. layout. simple_list_item_1 is a layout file defined by the system that only displays one line of text, a data source (a list set ). At the same time, use setadapter () to bind the listview and adapter.

 

Example 1: a text can be simply displayed.

Main. 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 = "fill_parent"
Android: Orientation = "vertical">

<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "cnblogs -- huarang"/>
<Listview
Android: Id = "@ + ID/listview"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>

</Linearlayout>

Activity

Package com. loulijun. demo14;

Import java. util. arraylist;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. widget. arrayadapter;
Import Android. widget. listview;

Public class demo13activity extends activity {
Private listview lv;
Private arraylist <string> List = new arraylist <string> ();
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Lv = (listview) findviewbyid (R. Id. listview );
Arrayadapter <string> adapter = new arrayadapter <string> (
This,
Android. R. layout. simple_expandable_list_item_1,
Getdata ());
LV. setadapter (adapter );
}

Private arraylist <string> getdata ()
{
List. Add ("180 square meters house ");
List. Add ("a hardworking and beautiful wife ");
List. Add ("a bmw ");
List. Add ("a strong and never ill ");
List. Add ("A favorite career ");
Return list;
}
}

Note: Here, Android. R. layout. simple_expandable_list_item_1 is the built-in layout of the system. The style is as follows. You can also customize the layout and import it into the layout, but only one textview can be used.

Very few codes. You can understand them at first glance. Some of the troubles may be caused by generics. If you don't understand them, you can refer to the basics of Java. The running effect is as follows:

Example 2: You can add an imageview in this example, but you need to add this custom layout when setting the arrayadapter.

Public arrayadapter (context, int resource, int textviewresourceid, list <t> objects)

The first parameter above is the context, generally this. The second parameter is a custom Layout file, for example, R. layout. list_item. The third parameter is the ID of textview, and the fourth parameter is data.

List_item.xml (main. XML is the same as above)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

</LinearLayout>

Activity

Package com. loulijun. demo14;

Import java. util. arraylist;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. widget. arrayadapter;
Import Android. widget. listview;

Public class demo13activity extends activity {
Private listview lv;
Private arraylist <string> List = new arraylist <string> ();
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Lv = (listview) findviewbyid (R. Id. listview );

Arrayadapter <string> adapter = new arrayadapter <string> (
This,
R. layout. list_item,
R. Id. TV,
Getdata ());
LV. setadapter (adapter );
}

Private arraylist <string> getdata ()
{
List. Add ("180 square meters house ");
List. Add ("a hardworking and beautiful wife ");
List. Add ("a bmw ");
List. Add ("a strong and never ill ");
List. Add ("A favorite career ");
Return list;
}
}

The arrayadapter and simpleadapter are also different here. It needs to set a custom layout in the adapter and then set the textview ID in it. However, it is very difficult to implement other controls, such as setting different graphs.

Baseadapter or simpleadaper is preferred for images with texts. Arrayadapter is suitable for some simple text information.

The effect is as follows:

Example 3: implement complex results

To be honest, this is a tough job to write, a lot of results cannot be achieved, or baseadapter is more omnipotent, but I still want to talk about how to use arrayadapter to implement complicated attempts. This requires rewriting the getview method, similar to baseadapter.

There are many advantages to override the getview method. For example, during listview optimization, operations in this method are mainly optimized. In addition, custom views are basically loaded using this method, to achieve your desired results

For details about this part, refer:

Http://www.cnblogs.com/sayo/archive/2010/12/30/1922590.html

Http://www.cnblogs.com/xirihanlin/archive/2009/08/03/1537609.html

Related Article

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.