Adding a ListView layout to a layout file
<?xml version= "1.0" encoding= "Utf-8"? ><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" android:paddingbottom= "@dimen/activity_vertical_margin" android: paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools:context= " Com.example.e5.duanxin.MainActivity "> <listview android:layout_width=" Fill_parent " android: layout_height= "Fill_parent" android:id= "@+id/lv" android:layout_alignparenttop= "true" Android: Layout_centerhorizontal= "true"/></relativelayout>
Create a new item layout file that can be edited according to your specific needs
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal" > <TextView android:layout_width= "0DP"Android:layout_weight= "1"Android:layout_height= "Wrap_content"Android:text= "Large Text"Android:id= "@+id/textview2"Android:textcolor= "#aa5599"/> <TextView android:layout_width= "0DP"Android:layout_weight= "1"Android:layout_height= "Wrap_content"Android:id= "@+id/textview"Android:text= "Body"Android:textcolor= "#FF5533"/></linearlayout>
ListView LV; List<Person> Personsinfos;
Configuring permissions in the Androidmanifest.xml file
<uses-permission android:name= "Android.permission.READ_SMS" ></uses-permission>
Declare a ListView layout and a list collection in mainactivity to hold the data
Get the ListView layout, and create a list collection object
Get the Contentresolver object
lv= (ListView) Findviewbyid (r.id.lv);
Personsinfos=new arraylist<person> ();
Contentresolver resolver= getcontentresolver ();
Find the URI of the mobile app "SMS"
Uri uri = uri.parse ("content://sms/");
Creates an object of the cursor class, puts the queried data into the cursor, and puts the data into the already built person class by MoveToNext ()
Finally close the cursor
Cursor cursor=resolver.query (URI,null,null,null,null); while (Cursor.movetonext ()) { person person =new person (); Person.setbody (Cursor.getstring (Cursor.getcolumnindex ("Body")); Person.settype (Cursor.getstring (Cursor.getcolumnindex ("Address")); Personsinfos.add (person);} Cursor.close ();
Creating the Baseadapter Parser
Create an inner class that inherits the Baseadapter interface and implements its abstract class
Private class Myadapter extends baseadapter{ @Override public int GetCount () { return personsinfos.size (); } @Override public Object getItem (int position) { return position; } @Override public long getitemid (int position) { return position; } @Override public View getView (int position, view Convertview, ViewGroup parent) { View view=view.inflate ( Mainactivity.this,r.layout.list_itme,null); TextView tv_b= (TextView) view. Findviewbyid (R.id.textview); TextView tv_t= (TextView) view. Findviewbyid (R.ID.TEXTVIEW2); Tv_t.settext (Personsinfos.get (position). GetType ()); Tv_b.settext (Personsinfos.get (position). GetBody ()); return view;} }
Load the adapter into the ListView layout
Lv.setadapter (new myadapter ());
Read the SMS database and put it in the ListView