Get a contact "your own definition layout file is connected to the main layout file, and the database content is looked up and displayed"

Source: Internet
Author: User

I. Define the layout file yourself List_item_users.xml

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
tools:context= "${packagename}.${activityclass}" >

<imageview
Android:id= "@+id/img"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@+id/first"
android:src= "@drawable/ic_launcher"/>

<textview
Android:id= "@+id/name"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignbottom= "@+id/img"
android:layout_torightof= "@+id/img"
android:text= "Name"/>

<textview
Android:id= "@+id/first"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparentleft= "true"
Android:layout_alignparenttop= "true"
android:text= "First"/>
</RelativeLayout>

Second, the main layout file Activity_main.xml layout a ListView

Third, database reading and content Association

public class Mainactivity extends Activity {

Private ListView lv_users;
Private Simplecursoradapter adapter;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Lv_users = (ListView) Findviewbyid (r.id.lv_users);

InitData ();
}

private void InitData () {

Contentresolver resolver =getcontentresolver ();
Cursor c is an indicator of the definition. Gets the contents of the database in query (table address, new string[]{to get the name of the column},. )
Cursor C = resolver.query (ContactsContract.RawContacts.CONTENT_URI,
New string[] {"_id", ContactsContract.Contacts.DISPLAY_NAME, "Phonebook_label"},
NULL, NULL, NULL);
The adapter adapter is a layout file that points to its own definition. (in context, define the layout file name yourself.) New string[]{What to lay out},new int[]{content})
adapter = new Simplecursoradapter (this, r.layout.list_item_users, C,
New string[] {ContactsContract.Contacts.DISPLAY_NAME, "Phonebook_label"}, new int[] {
R.id.name,r.id.first},
Cursoradapter.flag_register_content_observer);
Add an adapter (also understood as a layout file that you define yourself) to the main layout file
Lv_users.setadapter (adapter);
}
}

In Androidmanifest.xml you should pay attention to join permissions:

<instrumentation android:name= "Android.test.InstrumentationTestRunner" android:targetpackage= " Com.example.readcontactsdemo "></instrumentation>
<uses-permission android:name= "Android.permission.READ_CONTACTS"/>
<uses-permission android:name= "Android.permission.WRITE_CONTACTS"/>


android:minsdkversion= "11" changed from 8 to 11

Get a contact "your own definition layout file is connected to the main layout file, and the database content is looked up and displayed"

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.