Android Adapter Tutorial (ii)

Source: Internet
Author: User

Last time I wrote something like preface, I gave you a rough introduction of what an adapter is, and given a simple example to help you understand that using Android native Arrayadapter, Now we continue to learn, We also continue to add examples in our last demo project, stepping down and learning in depth.


This time to explain another common Android native adapter-simplecursoradapter:


As for Simplecursoradapter,theSDK explains this:

an easyadapter to map columns from a cursor to textviews or imageviews defined in Anxml file. You can specify which columns your want, which views you want todisplay the columns, and the XML file that defines the AppE Arance of Theseviews.


Simply put, it is convenient to display the data from the cursor, and can map the specified column to the corresponding TextView.

The following program is to write a contact from the phone book to the class table to get a cursor to the database and define a layout file (of course, you can also use the system's own) to show the data!

Project Start!


(1) or first in the Activity_main.xml to add a button, one will jump when used.

(2) Then create a new class Simplecursoradapterdemo inherits from activity as our second example of activity, and

@Override our method of onCreate .

(3) Create a new XML file Simplecursoradapterdemo.xml as our layout file, which also contains a text field and a ListView:

The code is as follows

Simplecursoradapterdemo.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:o rientation= "vertical" >    <textview        android:layout_width= "wrap_content"        android:layout_height= " Wrap_content "        android:text=" This is an example of Simplecursoradapter ">    </TextView>    <listview        Android:id= "@+id/simplecursoradapterlistview"        android:layout_width= "wrap_content"        android:layout_ height= "Wrap_content" >    </ListView></LinearLayout>

(4) back to Simplecursoradapterdemo, define a ListView to be called the LV, and use just the Id,setcontentview () for the XML layout just now, and then set an adapter for the LV:

SimpleCursorAdapter(Context context,intlayout,Cursor c, String[] from, int[] to)

first explain the parameters:

The first parameter, this, the second layout parameter is a single row in the ListView, and C is the cursor for your data. At first, from and to compare people unfamiliar, from is your query out of data, to is a single-line layout of the individual controls, one-to-one relationship, it is very convenient to use.


Again, the Cursoryou need to use:


We use:

cursor cursor = getcontentresolver (). query (People.content_uri, NULL, NULL, null,null);

Get a Cursor object pointing to the system Address Book database to get the data source.


Re-use:

Startmanagingcursor (cursor);

The cursor object is assigned to the activity management so that the cursor's life cycle and activity are automatically synchronized, eliminating the ability to manually manage the cursor.


Simplecursoradapter constructor The first 3 parameters are the same as the Arrayadapter, the last two arguments: A string array containing the database columns, and an int array containing the corresponding component IDs in the layout file. The effect is to automatically map each column of data represented by a string array to the component that corresponds to the ID of the layout file. The above code maps the data of the Name column once to the component with the ID of Text1 for the layout file.


Note: Required in androidmanifest.xml such as permissions:

<uses-permissionandroid:name= "Android.permission.READ_CONTACTS" ></uses-permission>


The specific code is as follows:

Package Com.example.adapterdemo;import Android.app.activity;import Android.database.cursor;import Android.os.bundle;import Android.provider.contacts.people;import Android.widget.listadapter;import Android.widget.listview;import Android.widget.simplecursoradapter;public class Simplecursoradapterdemo extends Activity{private ListView lv; @Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinst        Ancestate);        Setcontentview (R.layout.simplecursoradapterdemo);        LV = (ListView) Findviewbyid (R.id.simplecursoradapterlistview);        cursor cursor = getcontentresolver (). query (People.content_uri, NULL, NULL, NULL, and NULL);                 Startmanagingcursor (cursor); ListAdapter listadapter = new Simplecursoradapter (this, Android.                 R.layout.simple_expandable_list_item_1, cursor, new string[]{people.name}, New Int[]{android.                 R.ID.TEXT1}); Lv.setadapter (ListAdapter);}}


The final:





So the second commonly used Android native adapter will be over, or the spirit of a number of pieces to prevent the principle of too long, this one here, the next we continue to learn simpleadapter, a very extensible adapter, we do not miss Oh!


The source code will be written in the Last post, I also write a blog, while writing code, I think this way of thinking more clear.


I am also a student, the level of limited, but also hope that everyone's advice ~

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.