//查询:
//public final Cursor query(Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder);
//新增
//public final Uri insert(Uri url, ContentValues values)
//更新
/public final int update(Uri uri, ContentValues values, String where,
String[] selectionArgs)
//删除
//public final int delete(Uri url, String where, String[] selectionArgs)
//读取自己应用的短信记录程序
//读取手机内的短信内容,使用BaseAdapter适配器
public class MainActivity extends Activity {
private ListView listview;
private TextView empty;
private Cursor cursor;
private MybaseAdapter adapter;
private ContentResolver resolver;
// 短信数据库放在:手机的内存,/data/data/con.android.providers.telephony/databases/mmssms.db
private String uri = "content://sms";// 对外暴露的短信格式
private List<Map<String, String>> smsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) this.findViewById(R.id.listView_main_smslist);
empty = (TextView) this.findViewById(R.id.text_main_empty);
listview.setEmptyView(empty);
// 内容提供者获取数据库操作链接和数据库内的数据
// 通过Context类获取ContentResolver
resolver = getContentResolver();
//resolver.query(uri, projection, selection, selectionArgs, sortOrder)
//参数projection:数据库的字段;selection:sql语句;selectionArgs:想让sql插入更新等传入的内容 sortOrder:排序
cursor = resolver.query(Uri.parse(uri), new String[] { "_id",
"address", "body", "date", "type" }, null, null, null);
smsList = cursorToList(cursor);
// 建一个baseAdapter类
adapter = new MybaseAdapter(smsList);
listview.setAdapter(adapter);
}
public List<Map<String, String>> cursorToList(Cursor cursor2) {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
while (cursor2.moveToNext()) {// 注意:这里是循环每一行的数据
Map<String, String> smsItem = new HashMap<String, String>();
for (int i = 0; i < cursor2.getColumnCount(); i++) {// 这里是循环每一行的列数
smsItem.put(cursor2.getColumnName(i), cursor2.getString(i));// 键:key:存储每一行的字段,value:值就是内容
}
list.add(smsItem);
}
cursor2.close();
return list;
}
class MybaseAdapter extends BaseAdapter {
private List<Map<String, String>> smsList;
public MybaseAdapter(List<Map<String, String>> smsList) {
this.smsList = smsList;
}
@Override
public int getCount() {
return this.smsList.size();
}
@Override
public Object getItem(int position) {
return this.smsList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = getLayoutInflater().inflate(
R.layout.item_listview, null);
TextView text_address = (TextView) convertView
.findViewById(R.id.text_item_address);
TextView text_body = (TextView) convertView
.findViewById(R.id.text_item_body);
TextView text_date = (TextView) convertView
.findViewById(R.id.text_item_date);
ImageView image_type = (ImageView) convertView
.findViewById(R.id.imageView_item_icon);
holder.text_address = text_address;
holder.text_body = text_body;
holder.text_date = text_date;
holder.image_type = image_type;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// 设置UI控件的内容
holder.text_address.setText(this.smsList.get(position).get(
"address"));
holder.text_body.setText(this.smsList.get(position).get("body"));
long mills = Long.parseLong(this.smsList.get(position).get("date")
.toString());
SimpleDateFormat adf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
holder.text_date.setText(adf.format(new Date(mills)));
int type = Integer.parseInt(this.smsList.get(position).get("type")
.toString());
switch (type) {
case 1:
holder.image_type.setImageResource(R.drawable.file);
break;
case 2:
holder.image_type.setImageResource(R.drawable.folder);
break;
}
return convertView;
}
class ViewHolder {
private TextView text_address;
private TextView text_body;
private TextView text_date;
private ImageView image_type;
}
}
}
//主布局
<ListView
android:id="@+id/listView_main_smslist"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<TextView
android:id="@+id/text_main_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#00f"
android:textSize="24sp"
android:gravity="center"
android:text="暂无短信信息!" />
//适配器的布局
<ImageView
android:id="@+id/imageView_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text_item_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_toRightOf="@+id/imageView_item_icon"
android:text="TextView" />
<TextView
android:id="@+id/text_item_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView_item_icon"
android:layout_alignLeft="@+id/text_item_address"
android:text="TextView" />
<TextView
android:id="@+id/text_item_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="TextView" />
//读取电话的数据库记录的程序
public class MainActivity extends Activity {
private ListView listView;
private TextView empty;
private Cursor cursor;
private SimpleAdapter adapter;
private SimpleCursorAdapter cursorAdapter;
private ContentResolver resolver;
// 电话日志数据库放在:手机的内存,/data/data/con.android.providers.contacts/databases/contacts2.db
// private String uri = "content://call_log//calls";//这个也得
private String uri = "content://call_log/calls";// 对外暴露的电话记录格式,给外部程序调用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) this.findViewById(R.id.listView_main_calllist);
empty = (TextView) this.findViewById(R.id.text_main_empty);
listView.setEmptyView(empty);
// 内容提供者获取数据库操作链接和数据库内的数据
// 通过Context类获取ContentResolver
resolver = getContentResolver();
cursor = resolver.query(Uri.parse(uri), new String[] { "_id", "number",
"date", "type" }, null, null, null);
// 第一种:用SimpleAdapter
// 格式化日期
// SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// List<Map<String, String>> data = new ArrayList<Map<String,
// String>>();
// while (cursor.moveToNext()) {
// String number = cursor.getString(cursor.getColumnIndex("number"));
// long mills = cursor.getInt(cursor.getColumnIndex("date"));
// String date = sd.format(new Date(mills));
// String type = cursor.getString(cursor.getColumnIndex("type"));
// Map<String, String> itemMap = new HashMap<String, String>();
// itemMap.put("number", number);
// itemMap.put("date", date);
// itemMap.put("type", type);
// data.add(itemMap);
// }
//
// adapter = new SimpleAdapter(this, data, R.layout.item_listview,
// new String[] { "number", "date", "type" }, new int[] {
// R.id.text_item_number, R.id.text_item_date,
// R.id.text_item_type });
// listView.setAdapter(adapter);
// 第二种用:SimpleCursorAdapter
cursorAdapter = new SimpleCursorAdapter(this, R.layout.item_listview,
cursor, new String[] { "number", "date", "type" }, new int[] {
R.id.text_item_number, R.id.text_item_date,
R.id.text_item_type },
cursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);// 观察者
listView.setAdapter(cursorAdapter);
}
}
//主布局文件
<ListView
android:id="@+id/listView_main_calllist"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<TextView
android:id="@+id/text_main_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#00f"
android:textSize="24sp"
android:gravity="center"
android:text="暂无通话记录信息!" />
//SimpleAdapter自定义的布局
<TextView
android:id="@+id/text_item_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/text_item_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/text_item_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
转载于:https://my.oschina.net/u/2541146/blog/608655