Android learning notes-Adapter basics, android-adapter

Source: Internet
Author: User

Android learning notes-Adapter basics, android-adapter
This section introduces

Starting from this section, we will talk about the UI control that deals with the Adapter. It is very important to understand and learn how to use this Adapter. The Adapter is an intermediate bridge used to help fill data, to put it simply, various types of data are displayed on the view in the appropriate form and provided to users!

 

1. Adapter Concept Analysis

Official Document: Adapter

First, let's take a look at its inheritance structure:

The above is the Adapter and the inheritance structure. Next we will introduce several adapters used in actual development!

 
 
  • BaseAdapter: Abstract class. In actual development, we will inherit this class and rewrite the relevant methods. The most used Adapter!
  • ArrayAdapter: Supports generic operations. The simplest Adapter can only display one line of text ~
  • SimpleAdapter: An Adapter with good scalability, You Can Customize multiple effects!
  • SimpleCursorAdapter: Used to display listView of simple text type, which is usually used in the database, but a little outdated, not recommended!

In fact, a BaseAdapter is enough to play. As for others, it is not used much in actual development. It will be explained later ~

2. Sample Code:

Okay, I don't know much about it. Writing code is the most practical. Next we will write a few simple Adapter instances to help us understand the convenience that Adapter brings to us. In addition, because the Adapter needs to be integrated with ListView, gridView and other controls are explained. We will talk about some advanced usage in ListView! Here is a simple demonstration of the effect, and the control used here is ListView, which will be explained in the next section. It doesn't matter if you don't understand it now!

1) ArrayAdapter example:

Run:

Code implementation:

Public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // String [] strs = {"Ji Shen", "B Shen", "Xiang Shen", "Cao Shen", "J Shen "}; // create ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android. r. layout. simple_expandable_list_item_1, strs); // obtain the ListView object. Call the setAdapter method to set the Adapter for the ListView. Set the Adapter ListView list_test = (ListView) findViewById (R. id. list_test); list_test.setAdapter (adapter );}}

Some related things:

1.In addition to arrays, we can also write to an array resource file:

For example, create an xml file for Array resources under res \ valuse:Arrays. xml:

<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <string-array name = "myarray"> <item> language </item> <item> mathematics </item> <item> English </item> </string -array> </resources>

Next, set this list item in the listview attribute of the layout:

<ListView          android:id="@id/list_test"          android:layout_height="match_parent"          android:layout_width="match_parent"           android:entries="@array/myarray"/>

You can ~

Of course, we can also write this in Java code:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,        R.array.myarray,android.R.layout.simple_list_item_multiple_choice );

The same is true!

2.At the beginning, we also mentioned that this ArrayAdapter supports generics, so the set is indispensable. For example, write as follows:

List <String> data = new ArrayList <String> (); data. add ("God"); data. add ("B God"); ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android. r. layout. simple_expandable_list_item_1, data );

You can ~

3.We can see the second parameter in the instantiation of ArrayAdapter:Android. R. layout. simple_expandable_list_item_1In fact, these are some of the ListView templates provided by the system, which are as follows:

Simple_list_item_1: Text box with a single line

Simple_list_item_2: Composed of two text boxes

Simple_list_item_checked: Each item is composed of an selected list item.

Simple_list_item_multiple_choice: All contain a check box

Simple_list_item_single_choice: All have a single-choice button

2) SimpleAdapter example:

SimpleAdapter: simple Adapter, seemingly simple and powerful. Let's write a list layout that is a little more complex!

Run:

Code implementation:

First, compile a layout for each item of the list project:

List_item.xml

<? 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"> <! -- Define an ImageView for displaying an avatar --> <ImageView android: id = "@ + id/imgtou" android: layout_width = "64dp" android: layout_height = "64dp" android: baselineAlignBottom = "true" android: paddingLeft = "8dp"/> <! -- Define a vertical LinearLayout and set the QQ nickname and text box --> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "vertical"> <TextView android: id = "@ + id/name" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: paddingLeft = "8dp" android: textColor = "# 1D1D1C" android: textSize = "20sp"/> <TextView android: id = "@ + id/says" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: paddingLeft = "8px" android: textColor = "# B4B4B9" android: textSize = "14sp"/> </LinearLayout>

NextMainActivity. java:

Public class MainActivity extends AppCompatActivity {private String [] names = new String [] {"B God", "Ji Shen", "Cao Shen "}; private String [] says = new String [] {"invisible, most fatal", "Amazing ~ "," I will take the lead in the day dog ~ "}; Private int [] imgIds = new int [] {R. mipmap. head_icon1, R. mipmap. head_icon2, R. mipmap. head_icon3}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); List <Map <String, Object> listitem = new ArrayList <Map <String, Object> (); for (int I = 0; I <names. length; I ++) {Map <String, Object> showitem = new HashMap <String, Object> (); showitem. put ("touxiang", imgIds [I]); showitem. put ("name", names [I]); showitem. put ("says", says [I]); listitem. add (showitem);} // create a simpleAdapter SimpleAdapter myAdapter = new SimpleAdapter (getApplicationContext (), listitem, R. layout. list_item, new String [] {"touxiang", "name", "says"}, new int [] {R. id. imgtou, R. id. name, R. id. says}); ListView listView = (ListView) findViewById (R. id. list_test); listView. setAdapter (myAdapter );}}

Okay, it's simple to use SimpleAdapter ~

3) SimpleCursorAdapter instructions for use:

This is out of date. The general alternative is to rewrite the BaseAdapter and bind it to the corresponding control after obtaining the data set!

 

Summary:

Okay. Here is the basic description of the Adapter. Of course, the three adapters described here are actually being developed... basically not available, haha, except SimpleAdapter may occasionally be used, we generally rewrite BaseAdapter!
In addition, there are many things to explain about BaseAdapter, so I threw it to ListView to talk about it. After all, the Adapter is always side-by-side with the View, and ListView is the space we use most ~ Well, this section is here. Thank you ~

 

Reference from: http://www.runoob.com/w3cnote/android-tutorial-adapter.html

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.