标签:
在布局文件中加入ListView布局
<?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>
新建一个item布局文件,内容可根据具体需求编辑
<?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;
在AndroidManifest.xml文件中配置权限
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
在MainActivity中声明一个listView布局和一个List集合用于存放数据
得到ListView布局,和创建List集合对象
获得ContentResolver的对象
lv=(ListView) findViewById(R.id.lv);
personsInfos=new ArrayList<Person>();
ContentResolver resolver= getContentResolver();
找到手机应用程序"短信"的Uri
Uri uri = Uri.parse("content://sms/");
创建cursor类的对象,将查询得到的数据放到cursor中,通过moveToNext()将数据分别放入已经建好的Person类中
最后关闭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();
创建BaseAdapter解析器
创建一个内部类,继承BaseAdapter接口,并实现其抽象类
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; } }
将适配器加载的ListView布局里
lv.setAdapter(new MyAdapter());
标签:
原文地址:http://www.cnblogs.com/yboli/p/5674640.html