Android Controls ListView Usage (read contact sample code) _android

Source: Internet
Author: User
Tags stub
Sample code:
This is a code that reads the contact person:
Copy Code code as follows:

Package com.ui.domain;
Import java.util.ArrayList;
Import java.util.List;
Import android.app.Activity;
Import Android.database.Cursor;
Import Android.database.DataSetObserver;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import Android.provider.ContactsContract;
Import Android.util.Log;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.EditText;
Import Android.widget.ListAdapter;
Import Android.widget.ListView;
public class Callphone extends activity {



@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.call_phone);
Contact List
Final list<string> datalist=new arraylist<string> ();
Get all the Contacts
Cursor cur = getcontentresolver (). Query (
ContactsContract.Contacts.CONTENT_URI,
Null
Null
Null
ContactsContract.Contacts.DISPLAY_NAME
+ "COLLATE localized ASC");
while (Cur.movetonext ()) {
int idcolumn = Cur.getcolumnindex (contactscontract.contacts._id);
int displaynamecolumn = Cur.getcolumnindex (ContactsContract.Contacts.DISPLAY_NAME);
String phonenumber=cur.getstring (1);
String disPlayName = cur.getstring (Displaynamecolumn);
Datalist.add (displayname+ ":" +phonenumber);
LOG.I ("Contact name:", DisPlayName);
}
Cur.close ()//close cursor

ListView listview= (ListView) Findviewbyid (R.ID.LISTVIEW1);
Listview.setadapter (New ListAdapter () {

@Override
public void Unregisterdatasetobserver (Datasetobserver paramdatasetobserver) {
TODO auto-generated Method Stub

}

@Override
public void Registerdatasetobserver (Datasetobserver paramdatasetobserver) {
TODO auto-generated Method Stub

}

@Override
public Boolean IsEmpty () {
TODO auto-generated Method Stub
return false;
}

@Override
public Boolean hasstableids () {
TODO auto-generated Method Stub
return false;
}

@Override
public int Getviewtypecount () {
TODO auto-generated Method Stub
return Datalist.size ();
}

@Override
Public View getview (int paramint, View paramview, ViewGroup paramviewgroup) {
TODO auto-generated Method Stub
EditText text=new EditText (callphone.this);
Text.setbackgroundcolor (Color.White);
Text.settext (Datalist.get (paramint));
return text;
}

@Override
public int Getitemviewtype (int paramint) {
TODO auto-generated Method Stub
return 0;
}

@Override
public long getitemid (int paramint) {
TODO auto-generated Method Stub
return 0;
}

@Override
Public Object getitem (int paramint) {
TODO auto-generated Method Stub
Return Datalist.get (Paramint);
}

@Override
public int GetCount () {
TODO auto-generated Method Stub
return Datalist.size ();
}

@Override
public boolean isenabled (int paramint) {
TODO auto-generated Method Stub
return false;
}

@Override
public Boolean areallitemsenabled () {
TODO auto-generated Method Stub
return false;
}
});



}
@Override
protected void OnDestroy () {
TODO auto-generated Method Stub
Super.ondestroy ();
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
TODO auto-generated Method Stub
Getmenuinflater (). Inflate (R.menu.call_phone, menu);
return true;
}

}

This listview has a setadapter adapter that can directly implement the interface, or write a class that implements
Copy Code code as follows:

ListAdapter

This interface
Copy Code code as follows:

Listview.setadapter

In the definition of a list collection generic type string
There is a method in the interface:
Copy Code code as follows:

@Override
Public Object getitem (int paramint) {
TODO auto-generated Method Stub
Return Datalist.get (Paramint);
}

Return is the data returned to the list
Paramint, this is the index number.
Direct Fetch List.get (index)
That's it, okay?
Some of the above methods are also implemented
Note that if the list! >1 would have an error.
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.