Android control ListView usage (read the contact sample code)

Source: Internet
Author: User

Sample Code:
This is a code for reading contacts: Copy codeThe Code is 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> ();
// Obtain all 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 the 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 which can directly implement interfaces or write classes to implementCopy codeThe Code is as follows: ListAdapter

This interfaceCopy codeThe Code is as follows: listView. setAdapter

When defining a list set with a wildcard String
There is a method in the interface:Copy codeThe Code is as follows: @ Override
Public Object getItem (int paramInt ){
// TODO Auto-generated method stub
Return dataList. get (paramInt );
}

Return is the data returned from the list.
ParamInt. This is the index number.
Directly retrieve list. get (INDEX)
In this way, you can
We also need to implement the above several methods
NOTE: If list!> 1 will report 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.