Android UI Learning ListView (using Arrayadapter and Simpleadapter)

Source: Internet
Author: User

Since we are going to use Arrayadapter, let's look at the adapter relationship in the system first:

In practice the use of high frequency is: Simpleadapter, Arrayadapter, Baseadapter

Baseadapter: is an abstract class that implements a more versatile approach, but is highly flexible

Arrayadapter: Generic operation is supported, it is relatively simple to display only the same type of data

Simpleadapter: There's a better flexibility to define your own UI

About Baseadapter in the previous section, there is no illustration

Let's start with Arrayadapter:

public class Mainactivity extends Activity {    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        ListView ListView = (ListView) Findviewbyid (r.id.lv);               Listview.setadapter (New arrayadapter<string> (        mainactivity.this,         r.layout.item_list,//layout file for each element        R.id.tv_name,  //To set properties for that control in the layout file        new string[]{"video", "movie", "Wireless"});//Set properties for Id=tv_name           }

As follows:

As you can see, Arrayadapter can only set properties with the same ID, and the properties of other IDs are not set.

As a result, arrayadapter is typically used to set up only one type of data that is convenient and simple to set up.


Simpleadapter Study:

First put out the layout file above us:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:orientation= "horizontal" > <imageview android:id= "@+i        D/image_head "android:layout_width=" 50sp "android:layout_height=" 50sp "android:src=" @drawable/afh "         /> <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:orientation= "vertical" > <textview android:id= "@+id/tv_name" Androi     D:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "23sp"/> <textview android:id= "@+id/tv_phone" android:layout_width= "Wrap_content" android:layout_height= "W Rap_content "android:textsize=" 15sp "android:text=" 10086 "/> </linearlayout></linearla Yout>

Use Simpleadapter display:

 Define a List list<map<string, object>> data = new arraylist<map<string,object>> ();    The first row of data is set map<string, object> map1 = new hashmap<string, object> ();    Map1.put ("image", R.DRAWABLE.AFH);    Map1.put ("name", "positioning");    Map1.put ("Phone", "12345678900000000000");        Data.add (MAP1);    map<string, object> map2 = new hashmap<string, object> ();    Map2.put ("image", R.DRAWABLE.AFJ);    Map2.put ("name", "Music");    Map2.put ("Phone", "99999999999999999999");    Data.add (MAP2);    map<string, object> map3 = new hashmap<string, object> ();    Map3.put ("image", R.DRAWABLE.AFL);    Map3.put ("name", "Picture");    Map3.put ("Phone", "66666666666666666666");    Data.add (MAP3);    map<string, object> MAP4 = new hashmap<string, object> ();    Map4.put ("image", R.drawable.afn);    Map4.put ("name", "link");    Map4.put ("Phone", "88888888888888888888");    Data.add (MAP4); Listview.setadapter (New Simpleadapter (this, data, r.laYout.item_list,//Layout file style for each row new string[]{"image", "name", "Phone"},//Here the image and resource ID are one by one corresponding, the order is the corresponding new Int[]{r.id.imag E_head, R.id.tv_name, r.id.tv_phone});


The display results are as follows:

You can see the Simpleadapter function is still obvious, you can set an image, and 2 TextView




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android UI Learning ListView (using Arrayadapter and Simpleadapter)

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.