ContentProvider(内容提供者)简单运用例子

1、在配置清单里配置需要的权限

 

代码

 

 <uses-permission android:name="android.permission.READ_SMS"/>

 

 

 

========================================

 

2、布局界面有2个布局文件

 

activity_main.xml布局文件

 

代码

 

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <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="暂无短信信息!" />

</LinearLayout>

 

 

--------------------------

 

 

item_listview.xml布局文件

 

 

代码

 

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <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" />

</RelativeLayout>

 

 

=====================================

 

 

3、MainActivity 类

 

代码

 

 

public class MainActivity extends Activity {

 private ListView listview;
 private Cursor cursor;
 private TextView text_empty;
 private String uri_sms = "content://sms";
 private MyAdapter adapter;
 private List<Map<String, String>> list;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  this.listview = (ListView) this
    .findViewById(R.id.listView_main_smslist);
  this.text_empty = (TextView) this.findViewById(R.id.text_main_empty);
  listview.setEmptyView(text_empty);

  ContentResolver resolver = getContentResolver();// 内容提供者的一个方法
  cursor = resolver.query(Uri.parse(uri_sms), new String[] { "_id",
    "address", "body", "type", "date" }, null, null, null);
  list = select(cursor);

  adapter = new MyAdapter(list);
  listview.setAdapter(adapter);
 }
 // 查询方法
 public List<Map<String, String>> select(Cursor cursor) {
  List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  while (cursor.moveToNext()) {
   Map<String, String> map = new HashMap<String, String>();
   for (int i = 0; i < cursor.getColumnCount(); i++) {
    map.put(cursor.getColumnName(i), cursor.getString(i));
   }
   list.add(map);
  }
  return list;
 }

 class MyAdapter extends BaseAdapter {
  List<Map<String, String>> list;

  public MyAdapter(List<Map<String, String>> list) {
   this.list = list;
  }

  @Override
  public int getCount() {
   return this.list.size();
  }

  @Override
  public Object getItem(int position) {
   return this.list.get(position);
  }

  @Override
  public long getItemId(int position) {
   return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   ViewHolder viewholder = null;
   if (convertView == null) {
    viewholder = new ViewHolder();
    convertView = getLayoutInflater().inflate(
      R.layout.item_listview, null);

    viewholder.text_address = (TextView) convertView
      .findViewById(R.id.text_item_address);
    viewholder.text_body = (TextView) convertView
      .findViewById(R.id.text_item_body);
    viewholder.text_date = (TextView) convertView
      .findViewById(R.id.text_item_date);
    viewholder.image_type = (ImageView) convertView
      .findViewById(R.id.imageView_item_icon);

    convertView.setTag(viewholder);
   } else {
    viewholder = (ViewHolder) convertView.getTag();
   }
   // 设置 数据到listview
   viewholder.text_address.setText(list.get(position).get("address"));
   viewholder.text_body.setText(list.get(position).get("body"));
   // 格式化获取到的 时间
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
   long date = Long.parseLong(list.get(position).get("date"));
   String text_date = sdf.format(new Date(date));
   viewholder.text_date.setText(text_date);

   int type = Integer.parseInt(list.get(position).get("type"));
   switch (type) {
   case 1:
    viewholder.image_type.setImageResource(R.drawable.file);
    break;
   case 2:
    viewholder.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;
  }
 }
}



你可能感兴趣的:(ContentProvider(内容提供者)简单运用例子)