contentprovider_provider与contentprovider_resolver连接
程序员文章站
2022-07-05 14:22:49
...
contentprovider_provider的MainActivity什么也不写
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
MyContentProvider
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.Nullable;
/**
* @author alice
* @version 1.0
* @Date 2017/10/18 11:19
*/
public class MyContentProvider extends ContentProvider {
String path = "content://alice.bw.com.contentprovider_provider/student";
SQLiteDatabase db;
@Override
public boolean onCreate() {
MySQliteopenHelper openher = new MySQliteopenHelper(getContext());
db = openher.getReadableDatabase();
return false;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
Cursor c = db.query("student",strings,s,strings1,null,null,s1);
return c;
}
@Nullable
@Override
public String getType(Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
long insert = db.insert("student", null, contentValues);
Uri u = ContentUris.withAppendedId(Uri.parse(path),insert);
return u;
}
@Override
public int delete(Uri uri, String s, String[] strings) {
return 0;
}
@Override
public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
return 0;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
MySQliteopenHelper
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* @author alice
* @version 1.0
* @Date 2017/10/18 11:12
*/
public class MySQliteopenHelper extends SQLiteOpenHelper {
public MySQliteopenHelper(Context context) {
super(context, "student", null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table student(_id integer primary key autoincrement,name text,age text)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
activity_main的布局
<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="alice.bw.com.contentprovider_provider.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</RelativeLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
contentprovider_resolver的MainActivity
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private String path = "content://alice.bw.com.day16customcontentprovider/student";
private Button query_bt;
private Button insert_bt;
private Button delete_bt;
private ListView listView;
private ContentResolver mResolver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mResolver = getContentResolver();
initView();
}
private void initView() {
query_bt = (Button) findViewById(R.id.query_bt);
insert_bt = (Button) findViewById(R.id.insert_bt);
delete_bt = (Button) findViewById(R.id.delete_bt);
listView = (ListView) findViewById(R.id.listView);
query_bt.setOnClickListener(this);
insert_bt.setOnClickListener(this);
delete_bt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.query_bt:
queryStudent();
break;
case R.id.insert_bt:
insertStudent();
break;
case R.id.delete_bt:
break;
}
}
private void insertStudent() {
View dialogView = View.inflate(this, R.layout.dialog_layout, null);
final EditText name_et = (EditText) dialogView.findViewById(R.id.name_et);
final EditText age_et = (EditText) dialogView.findViewById(R.id.age_et);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher).setTitle("警告").setMessage("插入一个学生").setView(dialogView)
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = name_et.getText().toString();
String age = age_et.getText().toString();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("age", age);
mResolver.insert(Uri.parse(path), values);
}
});
builder.show();
}
// 查询
private void queryStudent() {
Cursor c = mResolver.query(Uri.parse(path), null, null, null, null);
ArrayList<Student> list = new ArrayList<Student>();
if (c != null) {
while (c.moveToNext()) {
String name = c.getString(c.getColumnIndex("name"));
String age = c.getString(c.getColumnIndex("age"));
Student stu = new Student(name,age);
list.add(stu);
}
}
listView.setAdapter(new StudentAdapter(this, list));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
Student
public class Student {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Student() {
}
public Student(String name, String age) {
this.name = name;
this.age = age;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
StudentAdapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* @author alice
* @version 1.0
* @Date 2017/10/18 11:50
*/
public class StudentAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Student> mList;
private LayoutInflater mInflater;
public StudentAdapter(Context context, ArrayList<Student> list) {
mContext = context;
mList = list;
mInflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
if(mList!=null){
return mList.size();
}
return 0;
}
@Override
public Student getItem(int i) {
if(mList!=null){
return mList.get(i);
}
return null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder vh = null;
if(view == null){
vh = new ViewHolder();
view = mInflater.inflate(R.layout.item_layout,null);
vh.name_tv = (TextView) view.findViewById(R.id.name_tv);
vh.age_tv = (TextView) view.findViewById(R.id.age_tv);
view.setTag(vh);
}else {
vh = (ViewHolder) view.getTag();
}
Student student = getItem(i);
vh.name_tv.setText(student.getName());
vh.age_tv.setText(student.getAge());
return view;
}
class ViewHolder{
TextView name_tv,age_tv;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
StudentAdapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* @author alice
* @version 1.0
* @Date 2017/10/18 11:50
*/
public class StudentAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Student> mList;
private LayoutInflater mInflater;
public StudentAdapter(Context context, ArrayList<Student> list) {
mContext = context;
mList = list;
mInflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
if(mList!=null){
return mList.size();
}
return 0;
}
@Override
public Student getItem(int i) {
if(mList!=null){
return mList.get(i);
}
return null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder vh = null;
if(view == null){
vh = new ViewHolder();
view = mInflater.inflate(R.layout.item_layout,null);
vh.name_tv = (TextView) view.findViewById(R.id.name_tv);
vh.age_tv = (TextView) view.findViewById(R.id.age_tv);
view.setTag(vh);
}else {
vh = (ViewHolder) view.getTag();
}
Student student = getItem(i);
vh.name_tv.setText(student.getName());
vh.age_tv.setText(student.getAge());
return view;
}
class ViewHolder{
TextView name_tv,age_tv;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
activity_main布局
<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"
tools:context="alice.bw.com.contentprovider_resolver.MainActivity">
<Button
android:id="@+id/query_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="查询" >
</Button>
<Button
android:id="@+id/insert_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/query_bt"
android:onClick="onClick"
android:text="插入" >
</Button>
<Button
android:id="@+id/delete_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/insert_bt"
android:onClick="onClick"
android:text="删除" >
</Button>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/insert_bt" >
</ListView>
</RelativeLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
dialog_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/name_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入名字" >
</EditText>
<EditText
android:id="@+id/age_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入年龄" />
</LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
item_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/name_tv"
android:layout_width="wrap_content"
android:textSize="18sp"
android:layout_margin="5dp"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/age_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="TextView"
android:textSize="18sp" />
</LinearLayout>