Android Learning Adapter Adapter

Source: Internet
Author: User

What is the role of adapter? In fact, it is a bridge between the view interface and the data. We can be seen as an understanding of interface data binding, and the data it manipulates are generally more complex data, such as arrays, lists, databases, collections, etc.

The commonly used adapters are:

(1) arrayadapter; array as the data source, filled with arrayadapter

(2) Simpleadapter; list as a data source, populated with Simpleadapter

(3) Simplecursoradapter; Data sources are usually the cursor that the database query gets

(4) custom adapter; Why do you define your own adapter? The reason is, when we want to use some other way of presentation, or we need to present the way, it is necessary to DIY. First we define a class to inherit from Baseadapter, and then let it implement a few of the methods described in it. Then this custom adapter is good.

Let's take a look at the following example to understand:

One. Arrayadapter

1.xml Code:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Fill_parent"5 android:orientation= "vertical" >6 7     <ListView8         Android:id= "@+id/myarraylist"9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Wrap_content" /> One  A </LinearLayout>

2.java Code:

1  PackageCom.example.arrayadapter;2 3 Importjava.util.ArrayList;4 Importandroid.app.Activity;5 ImportAndroid.os.Bundle;6 ImportAndroid.widget.ArrayAdapter;7 ImportAndroid.widget.ListView;8 9  Public classMainactivityextendsActivity {Ten     PrivateListView Mlistview; One     PrivateArraylist<string> marraylist =NULL; A  - @Override -     protected voidonCreate (Bundle savedinstancestate) { the         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -Mlistview =(ListView) Findviewbyid (r.id.myarraylist); -Mlistview.setadapter (NewArrayadapter<string> ( This, + Android. R.layout.simple_expandable_list_item_1, GetData ())); -     } +  A     PrivateArraylist<string>GetData () { atMarraylist =NewArraylist<string>(); -Marraylist.add ("Item1"); -Marraylist.add ("Item2"); -Marraylist.add ("Item3"); -Marraylist.add ("Item4"); -Marraylist.add ("ITEM5"); inMarraylist.add ("Item6"); -Marraylist.add ("Item7"); toMarraylist.add ("ITEM8"); +Marraylist.add ("Item9"); -Marraylist.add ("ITEM10"); the         returnmarraylist; *     } $}

Two.

Android Learning Adapter Adapter

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.