Feel:
1. Learned about the action and URI in intent.
2. Understand the next activity passing data.
3. Understand the content provider.
4. Learn about custom adapters.
4. Actually the T9 dialer is the same as the simple calculator principle.
Code:
Mainactivity:
Packagecom.example.contactstest;Importjava.util.ArrayList;Importjava.util.List;ImportAndroid.os.Bundle;Importandroid.provider.ContactsContract;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.Window;ImportAndroid.widget.AdapterView;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView;ImportAndroid.widget.Toast;ImportAndroid.widget.AdapterView.OnItemClickListener;Importandroid.app.Activity;Importandroid.app.DownloadManager.Request;Importandroid.content.Intent;ImportAndroid.database.Cursor; Public classMainactivityextendsActivity {ListView contactsview; List<Contact> contactslist =NewArraylist<contact>(); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); Contactsview=(ListView) Findviewbyid (R.id.contacts_view); Contact_item Adapter=NewContact_item ( This, R.layout.item, contactslist); Contactsview.setadapter (adapter); Readcontacts (); Contactsview.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?>Parent, view view,intPositionLongID) {Contact fruit=Contactslist.get (position); String Data=Fruit.getnumber (); Intent I=NewIntent (mainactivity. This, Tel.class); I.putextra ("Tel_number", data); StartActivity (i); } }); } Private voidreadcontacts () {cursor cursor=NULL; Try{cursor=getcontentresolver (). Query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL,NULL,NULL,NULL); while(Cursor.movetonext ()) {String DisplayName=cursor.getstring (cursor. Getcolumnindex (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String Number=cursor.getstring (cursor. Getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER)); LOG.D ("Mainactivity", "name is" +displayName); Contact a=NewContact (displayname,number); Contactslist.add (a); } } Catch(Exception e) {e.printstacktrace (); } finally { if(Cursor! =NULL) {cursor.close (); } } }}
View Code
Tel:
Packagecom.example.contactstest;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.Window;ImportAndroid.widget.Button;ImportAndroid.widget.EditText; Public classTelextendsactivity{String number; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.tel); Intent Intent=getintent (); number= Intent.getstringextra ("Tel_number"); Button Btncall=(Button) Findviewbyid (R.id.btn_call); EditText Etnumber=(EditText) Findviewbyid (R.id.number); Etnumber.settext (number); Etnumber.setselection (Etnumber.gettext (). Length ());//position the cursor to the lastBtncall.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//System.out.println ("MainUI2 calls."); //log.d ("Tel", number);Call (); } }); } Private voidCall () {//log.d ("Mainui", "call");Intent Intent=NewIntent (); Intent.setaction (Intent.action_call); Intent.setdata (Uri.parse ("Tel:" +Number )); StartActivity (Intent); }}
View Code
Contact:
Packagecom.example.contactstest; Public classContact {PrivateString name; PrivateString number; PublicContact (string name, string number) { This. Name =name; This. Number =Number ; } PublicString GetName () {returnname; } PublicString GetNumber () {returnNumber ; }}
View Code
Contact_item:
Packagecom.example.contactstest;Importjava.util.List;ImportAndroid.content.Context;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView; Public classContact_itemextendsArrayadapter<contact>{ Private intresourceId; PublicContact_item (Context context,intTextviewresourceid, List<Contact>objects) { Super(Context, Textviewresourceid, objects); ResourceId=Textviewresourceid; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {Contact Fruit=GetItem (position); View view; Viewholder Viewholder; if(Convertview = =NULL) {View= Layoutinflater.from (GetContext ()). Inflate (ResourceId,NULL); Viewholder=NewViewholder (); Viewholder.fruitnumber=(TextView) View.findviewbyid (R.id.number); Viewholder.fruitname=(TextView) View.findviewbyid (r.id.name); View.settag (Viewholder); } Else{View=Convertview; Viewholder=(Viewholder) View.gettag (); } viewHolder.fruitNumber.setText (Fruit.getnumber ()); ViewHolder.fruitName.setText (Fruit.getname ()); returnview; } classViewholder {TextView fruitnumber; TextView Fruitname; } }
View Code
Simple Dialer (Android)