Android adapter tutorial (2)

Source: Internet
Author: User

Android adapter tutorial (2)

The last time I wrote something similar to the preface, I gave you a rough introduction to what is an adapter, and gave you a simple example to help you understand it. I used the android native ArrayAdapter.,Now let's continue to learn further, and add examples in our previous Demo project, step by step.


This is another common android native adapter-SimpleCursorAdapter:


For SimpleCursorAdapter, the sdk is interpreted as follows:

An easyadapter to map columns from a cursor to TextViews or ImageViews defined in anXML file. you can specify which columns you want, which views you want todisplay the columns, and the XML file that defines the appearance of theseviews.


Simply put, the data obtained from the cursor is displayed in the list and the specified columns can be mapped to the corresponding TextView.

The program to be written below is to display the contact in the phone book to the class table to get a Cursor pointing to the database and define a layout file (of course, you can also use the built-in system) to display the data!

Project started!


(1)Add a button in activity_main.xml for later jump.

(2)Create a new class SimpleCursorAdapterDemo inherited from Activity as the Activity in our second example, and

@ Override ourOnCreateMethod.

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

The Code is as follows:

Simplecursoradapterdemo. xml:

 
     
      
      
      
  
 

(4)Return to SimpleCursorAdapterDemo, define a Listview that is currently called lv, and use the ID, setContentView () for the xml layout, and then set an adapter for lv:

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

First, explain the parameters:

The first parameter this, the second layout parameter is the layout of a single row in ListView, and c is your data cursor. In the beginning, from and to are quite unfamiliar. From is the data you have queried. to is a separate control in a single row layout. It has a one-to-one relationship and is very convenient to use.


Let's talk about what we needCursor:


We use:

Cursor cursor = getContentResolver (). query (People. CONTENT_URI, null );

First obtain a Cursor object pointing to the system Address Book database to obtain the data source.


Reuse:

StartManagingCursor (cursor );

The obtained Cursor object is handed over to the Activity for management, so that the life cycle and Activity of the Cursor can be automatically synchronized, eliminating the need to manually manage the Cursor.


The first three parameters of the SimpleCursorAdapter constructor are the same as those of the ArrayAdapter. The last two parameters are: a String array containing the database columns and an int array containing the corresponding component id in the layout file. The function is to automatically map each column of data represented by the String array to the component with the corresponding id of the layout file. The code above maps the data in the NAME column to the component whose id is text1 in the layout file once.


Note: You must have the following permissions in AndroidManifest. xml:


The 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(savedInstanceState);        setContentView(R.layout.simplecursoradapterdemo);        lv = (ListView)findViewById(R.id.simplecursoradapterlistview);        Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, 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);}}


Final:


The source code will be uploaded at the end of the article. I also write a blog and code. I think this idea is clearer.


I am also a student and have a limited level. I hope you can give me more advice ~

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.