实例解析Android系统中的ContentProvider组件用法

2019-12-10 18:12:36于海丽

item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://www.easck.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="horizontal" > 
  <TextView  
    android:id="@+id/item_txt1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
    <TextView  
    android:id="@+id/item_txt2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
   
 
</LinearLayout> 

B程序
UserInfo.java

package com.android.xiong.contentprovidertestb; 
 
//向外部程序提供一个工具类 
public class UserInfo { 
 
  // 获取ContentProvider的“域名” 
  public static final String AUTOR = "com.android.xiong.ConentProviderTestA.firstContentProvider"; 
   
  //定义一个静态内部类,提供ContentProvider可操作的列 
  public static final class User { 
     
    public static final String ID="id"; 
    public static final String NAME="name"; 
    public static final String AGE="age"; 
    //定义该content提供服务的一个Uri 
    public static final String uri="content://"+AUTOR+"/userinfo"; 
    public static final String uriall="content://"+AUTOR+"/userinfoall"; 
     
  } 
 
} 

MainActivity.java

package com.android.xiong.contentprovidertestb; 
 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
 
import android.app.Activity; 
import android.content.ContentValues; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.ScrollView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 
 
public class MainActivity extends Activity { 
 
  private Button bt1, bt2, bt3, bt4; 
  private EditText ed1, ed2; 
  private ListView list1; 
  private ScrollView sc1; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    bt1 = (Button) findViewById(R.id.bt1); 
    bt2 = (Button) findViewById(R.id.bt2); 
    bt3 = (Button) findViewById(R.id.bt3); 
    bt4 = (Button) findViewById(R.id.bt4); 
    ed1 = (EditText) findViewById(R.id.ed1); 
    ed2 = (EditText) findViewById(R.id.ed2); 
    list1 = (ListView) findViewById(R.id.list); 
    // 显示所有数据 
    list1.setAdapter(adapter(0)); 
    sc1 = (ScrollView) findViewById(R.id.scr1); 
    // 向添加ContentProviderA应用的数据 
    bt1.setOnClickListener(new OnClickListener() { 
 
      @Override 
      public void onClick(View v) { 
        String eds1 = ed1.getText().toString(); 
        String eds2 = ed2.getText().toString(); 
        ContentValues content = new ContentValues(); 
        if (!eds1.equals("") && !eds2.equals("")) { 
          content.put(UserInfo.User.NAME, eds1); 
          content.put(UserInfo.User.AGE, eds2); 
          MainActivity.this.getContentResolver().insert( 
              Uri.parse(UserInfo.User.uri), content); 
          Toast.makeText(MainActivity.this, "数据插入成功", 
              Toast.LENGTH_LONG).show(); 
          // 刷新ListView界面 
          list1.setAdapter(adapter(0)); 
        } else { 
          Toast.makeText(MainActivity.this, "name和age不能为空", 
              Toast.LENGTH_LONG).show(); 
        } 
 
      } 
    }); 
    // 根据条件删除ContentProviderA应用的数据 
    bt2.setOnClickListener(new OnClickListener() { 
 
      @Override 
      public void onClick(View v) { 
        String eds1 = ed1.getText().toString(); 
        String eds2 = ed2.getText().toString(); 
        if (!eds1.equals("") || !eds2.equals("")) { 
          HashMap<String, String[]> wheres = wheres(eds1, eds2); 
          String sql = wheres.get("sql")[0]; 
          String[] selectags = wheres.get("selectages"); 
          MainActivity.this.getContentResolver().delete( 
              Uri.parse(UserInfo.User.uri), sql, selectags); 
 
        } else { 
          Toast.makeText(MainActivity.this, "请输入删除条件", 
              Toast.LENGTH_LONG).show(); 
        } 
        // 刷新ListView界面 
        list1.setAdapter(adapter(0)); 
      } 
    }); 
    // 修改数据 
    bt3.setOnClickListener(new OnClickListener() { 
 
      @Override 
      public void onClick(View v) { 
        String eds1 = ed1.getText().toString(); 
        String eds2 = ed2.getText().toString(); 
        ContentValues values = new ContentValues(); 
        // 根据条件将列修改为xiong,23 
        values.put(UserInfo.User.NAME, "xiong"); 
        values.put(UserInfo.User.AGE, "23"); 
        if (!eds1.equals("") || !eds2.equals("")) { 
          HashMap<String, String[]> wheres = wheres(eds1, eds2); 
          String sql = wheres.get("sql")[0]; 
          String[] selectags = wheres.get("selectages"); 
          int i=MainActivity.this.getContentResolver().update( 
              Uri.parse(UserInfo.User.uri), values, sql, 
              selectags); 
 
        } else { 
          Toast.makeText(MainActivity.this, "请输入删除条件", 
              Toast.LENGTH_LONG).show(); 
        } 
        // 刷新ListView界面 
        list1.setAdapter(adapter(0)); 
 
      } 
    }); 
    // 根据条件查询ContentProviderA应用的数据 
    bt4.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        if (!ed1.getText().toString().equals("") 
            || !ed2.getText().toString().equals("")) 
          list1.setAdapter(adapter(1)); 
        else 
          list1.setAdapter(adapter(0)); 
      } 
    }); 
  } 
 
  // 用来判别条件 
  public HashMap<String, String[]> wheres(String eds1, String eds2) { 
    HashMap<String, String[]> where = new HashMap<String, String[]>(); 
    if (!eds1.equals("") && !eds2.equals("")) { 
      String[] sql = { UserInfo.User.NAME + "=? and " + UserInfo.User.AGE 
          + " =?" }; 
      String[] selectages = { eds1, eds2 }; 
      where.put("sql", sql); 
      where.put("selectages", selectages); 
 
    } 
    if (!eds1.equals("") && eds2.equals("")) { 
      String[] sql = { UserInfo.User.NAME + "=? " }; 
      String[] selectages = { eds1 }; 
      where.put("sql", sql); 
      where.put("selectages", selectages); 
 
    } 
    if (eds1.equals("") && !eds2.equals("")) { 
      String[] sql = { UserInfo.User.AGE + " =?" }; 
      String[] selectages = { eds2 }; 
      where.put("sql", sql); 
      where.put("selectages", selectages); 
 
    } 
    return where; 
  } 
 
  // 用来显示数据 
  public SimpleAdapter adapter(int i) { 
    Cursor cs = MainActivity.this.getContentResolver().query( 
        Uri.parse(UserInfo.User.uriall), null, null, null, null); 
    String eds1 = ed1.getText().toString(); 
    String eds2 = ed2.getText().toString(); 
    if (i == 1) { 
      if (!eds1.equals("") || !eds2.equals("")) { 
        HashMap<String, String[]> wheres = wheres(eds1, eds2); 
        String sql = wheres.get("sql")[0]; 
        String[] selectags = wheres.get("selectages"); 
        cs = MainActivity.this.getContentResolver().query( 
            Uri.parse(UserInfo.User.uri), null, sql, selectags, 
            null); 
      } 
 
    } 
 
    List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>(); 
    while (cs.moveToNext()) { 
      Map<String, Object> map = new HashMap<String, Object>(); 
      map.put("name", cs.getString(0)); 
      map.put("age", cs.getString(1)); 
      lists.add(map); 
    } 
    SimpleAdapter simepl = new SimpleAdapter(MainActivity.this, lists, 
        R.layout.item, new String[] { "name", "age" }, new int[] { 
            R.id.item_txt1, R.id.item_txt2 }); 
    return simepl; 
  } 
 
  @Override 
  public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
  } 
 
}