//清单文件中的注册
android:name=".MyContentProvider"
android:exported="true"
/>
public class MyContentProvider extends ContentProvider {
}
//创建库
public class DbOpenHelper extends SQLiteOpenHelper {
public DbOpenHelper(Context context) {
//初始化表
super(context, "bw", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//初始表
db.execSQL("create table user(_id integer primary key autoincrement,uName,uPhone)");
db.execSQL("insert into user(uName,uPhone) values('乔博','15888888888')");
db.execSQL("insert into user(uName,uPhone) values('小马','15666666666')");
db.execSQL("insert into user(uName,uPhone) values('小郭','15222222222')");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
//以下是另一个Mode中的内容
public class MainActivity extends AppCompatActivity {
private ListView mLv;
private Cursor cursor;//数据源
private SimpleCursorAdapter adapter;//适配器
private ContentResolver resolver;
private Uri uri = Uri.parse("content://bw.com.bw_day22.bw/user");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLv = (ListView) findViewById(R.id.lv_id);
//声明解析器
resolver = getContentResolver();
}
@Override
protected void onResume() {
super.onResume();
cursor = resolver.query(uri,null,null,null,null);
adapter = new SimpleCursorAdapter(this,
R.layout.item_lv,
cursor,
new String[]{"uName","uPhone"},
new int[]{R.id.name_tv,R.id.phone_tv},
SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
mLv.setAdapter(adapter);
mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, final int position, long id) {
//弹出对话框
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("请选择");
String[] items = {"编辑联系人","删除联系人"};
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which)
{
case 0:
//编辑联系人
//获取当前条目的内容
if(cursor.moveToPosition(position))
{
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("uName"));
String phone = cursor.getString(cursor.getColumnIndex("uPhone"));
//携带参数
Intent intent = new Intent(MainActivity.this,EditActivity.class);
intent.putExtra("id",id);
intent.putExtra("name",name);
intent.putExtra("phone",phone);
startActivity(intent);
}
break;
case 1:
//删除联系人
if (cursor.moveToPosition(position))
{
AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
builder1.setIcon(R.mipmap.ic_launcher);
builder1.setTitle("提示");
builder1.setMessage("是否确认删除?");
builder1.setNegativeButton("取消",null);
builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int id = cursor.getInt(cursor.getColumnIndex("_id"));
resolver.delete(uri,"_id=?",new String[]{id+""});
cursor.requery();//再次执行查询的方法
}
});
builder1.show();
}
break;
}
}
});
builder.show();
}
});
}
//创建菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.action_add:
Intent intent = new Intent(this,EditActivity.class);
startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
}
///另一个类
public class EditActivity extends AppCompatActivity {
private EditText nameEt;
private EditText phoneEt;
private ContentResolver resolver;
private Uri uri = Uri.parse("content://bw.com.bw_day22.bw/user");
private int id = -1;
private String uName,uPhone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
nameEt = (EditText) findViewById(R.id.name_et);
phoneEt = (EditText) findViewById(R.id.phone_et);
resolver = getContentResolver();
//编辑功能
//获取传入的参数
Intent intent = getIntent();
id = intent.getIntExtra("id",-1);
uName = intent.getStringExtra("name");
uPhone = intent.getStringExtra("phone");
nameEt.setText(uName);
phoneEt.setText(uPhone);
}
public void submit(View view)
{
//获取输入框的内容
String name = nameEt.getText().toString().trim();
String phone = phoneEt.getText().toString().trim();
//添加数据
ContentValues values = new ContentValues();
values.put("uName",name);
values.put("uPhone",phone);
if (id!=-1)
{
//修改
resolver.update(uri,values,"_id=?",new String[]{id+""});
}
else{
//添加
resolver.insert(uri,values);
}
finish();
}
}