通过ContentResolver操作内容提供者的数据,将姓名,电话显示在listView中,并带有添加和删除按钮进行相应操作
<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=".MainActivity" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbarStyle="outsideInset" > </ListView> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/btn_delete" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="5dp" android:layout_marginLeft="5dp" android:layout_marginRight="10dp" android:background="@drawable/del" android:onClick="delete" /> <Button android:id="@+id/btn_update" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/btn_delete" android:onClick="update" android:layout_margin="5dp" android:background="@drawable/refresh" /> <TextView android:id="@+id/tv_name" android:layout_width="60dp" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/btn_update" android:layout_alignBottom="@+id/btn_update" android:layout_alignParentLeft="true" android:paddingLeft="10dp" android:text="TextView" /> <TextView android:id="@+id/tv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/tv_name" android:layout_toRightOf="@+id/tv_name" android:paddingLeft="10dp" android:text="TextView" /> </RelativeLayout>
<LinearLayout 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:layout_margin="10dp" android:orientation="vertical" tools:context=".UserUpdateActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tv_upd_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户编号" /> <EditText android:id="@+id/ed_upd_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:ems="10" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tv_upd_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名称" /> <EditText android:id="@+id/ed_upd_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:ems="10" > <requestFocus /> </EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tv_upd_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户年龄" /> <EditText android:id="@+id/ed_upd_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:ems="10" android:inputType="number" > <requestFocus /> </EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tv_upd_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户电话" /> <EditText android:id="@+id/ed_upd_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:ems="10" android:inputType="phone" > <requestFocus /> </EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tv_upd_salary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户薪水" /> <EditText android:id="@+id/ed_upd_salary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:ems="10" android:inputType="numberDecimal" > <requestFocus /> </EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_marginTop="20dp" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="140dp" android:layout_height="wrap_content" android:background="@drawable/btn_bg" android:onClick="update" android:text="更新用户" android:gravity="center" /> </LinearLayout> </LinearLayout>
package com.example.android_provider; import android.app.Activity; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.pm.LabeledIntent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.v4.widget.CursorAdapter; import android.support.v4.widget.SimpleCursorAdapter; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private ListView lv_user; // 内容解析器 private ContentResolver contentResolver; // 操作的标志 private static final String AUTHORITIE = "www.csdn.com.provider.userContentProvider"; // 请求码 int requestUpdCode = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 模型层 // 创建内容解析器对象 contentResolver = getContentResolver(); lv_user = (ListView) findViewById(R.id.listView1); initData(); } /** * 查找数据 */ private void initData() { // 控制层(自定义) Cursor c = contentResolver.query( Uri.parse("content://" + AUTHORITIE + "/query"), new String[] { "userid as _id", "username", "userage", "usersalary", "userphone" }, null, null, null); MyCursorAapter adapter = new MyCursorAapter(this, c, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); // 视图层 lv_user.setAdapter(adapter); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // 请求码 if (requestCode == requestUpdCode) { // 结果码 if (resultCode == 2) { // 传过来的数据 if (data.getIntExtra("count", 0) > 0) { initData();// 重新加载界面 Toast.makeText(this, "更新成功", 0).show(); } else { Toast.makeText(this, "更新失败", 0).show(); } } } } class MyCursorAapter extends CursorAdapter { // 上下文对象 private Context context; // 打气筒,布局服务对象 private LayoutInflater mInflater; // 构造器 public MyCursorAapter(Context context, Cursor c, int flags) { super(context, c, flags); this.context = context; mInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } // mInflater服务通过inflate方法返回一个View控件对象 // 通过自己的布局文件转化的View对象,转化之后就可以在bindView处理 // 先new出当前的布局。new出每一个条目的布局文件。 // new出当前布局,bindView中就可以用findViewById去拿控件 @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return mInflater.inflate(R.layout.list_item_user, parent, false); } @Override public void bindView(View view, Context context, Cursor cursor) { // 查找组件 Button btn_del = (Button) view.findViewById(R.id.btn_delete); Button btn_upd = (Button) view.findViewById(R.id.btn_update); TextView tv_name = (TextView) view.findViewById(R.id.tv_name); TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone); // 设置值 tv_name.setText(cursor.getString(cursor.getColumnIndex("username"))); tv_phone.setText(cursor.getString(cursor .getColumnIndex("userphone"))); // 根据id删除 final int _id = cursor.getInt(cursor.getColumnIndex("_id")); final String name = cursor.getString(cursor .getColumnIndex("username")); // 删除按钮的注册事件 btn_del.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 通过内容解析器删除 int count = contentResolver.delete( Uri.parse("content://" + AUTHORITIE + "/delete"), "userid=?", new String[] { _id + "" }); if (count > 0) { Toast.makeText(MainActivity.this, "成功删除掉" + name, Toast.LENGTH_LONG).show(); // 删除后重新加载 initData(); } } }); // 根据每条更新 final String age = cursor.getString(cursor .getColumnIndex("userage")); final String salary = cursor.getString(cursor .getColumnIndex("usersalary")); final String phone = cursor.getString(cursor .getColumnIndex("userphone")); // 更新按钮的注册事件 btn_upd.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("uid", _id); intent.putExtra("uname", name); intent.putExtra("uage", age); intent.putExtra("usalary", salary); intent.putExtra("uphone", phone); intent.setClass(getApplicationContext(), UserUpdateActivity.class); startActivityForResult(intent, requestUpdCode); } }); } } }
package com.example.android_provider; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class UserUpdateActivity extends Activity { // 内容解析器 private ContentResolver contentResolver; // 操作的标志 private static final String AUTHORITIE = "www.csdn.com.provider.userContentProvider"; private EditText etName, etAge, etSalary, etId, etPhone; // 结果码 private int resultCode = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_user_update); findView(); // 获取意图对象 Intent intent = getIntent(); // 给控件设置intent传过来的值 是数字都必须加“”转为字符串 etId.setText(intent.getIntExtra("uid", 0) + ""); etName.setText(intent.getStringExtra("uname")); etAge.setText(intent.getIntExtra("uage", 18) + ""); etSalary.setText(intent.getDoubleExtra("usalary", 0) + ""); etPhone.setText(intent.getStringExtra("uphone")); // 创建解析器对象 contentResolver = getContentResolver(); } // 查找控件 public void findView() { etId = (EditText) findViewById(R.id.ed_upd_id); etName = (EditText) findViewById(R.id.ed_upd_name); etAge = (EditText) findViewById(R.id.ed_upd_age); etSalary = (EditText) findViewById(R.id.ed_upd_salary); etPhone = (EditText) findViewById(R.id.ed_upd_phone); } public void update(View v) { String uid = etId.getText().toString().trim(); String uname = etName.getText().toString().trim(); String uage = etAge.getText().toString().trim(); String usalart = etSalary.getText().toString().trim(); String uphone = etPhone.getText().toString().trim(); // values.put(key, value)put的key值必须是数据库里的值 ContentValues values = new ContentValues(); values.put("userid", uid); values.put("username", uname); values.put("userage", uage); values.put("usersalary", usalart); values.put("userphone", uphone); int count = contentResolver.update( Uri.parse("content://" + AUTHORITIE + "/update"), values, "userid=?", new String[] { uid }); Intent intent = new Intent(); intent.putExtra("count", count); UserUpdateActivity.this.setResult(resultCode, intent); // 关闭资源 this.finish(); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_provider" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.android_provider.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.android_provider.UserUpdateActivity" android:theme="@android:style/Theme.Dialog" > </activity> </application> </manifest>
<span style="white-space:pre"> </span>// 构造器 <span style="white-space:pre"> </span>public MyCursorAapter(Context context, Cursor c, int flags) { <span style="white-space:pre"> </span>super(context, c, flags); <span style="white-space:pre"> </span>this.context = context; <span style="white-space:pre"> </span>mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); <span style="white-space:pre"> </span>}
mInflater充当打气筒的角色,布局服务对象
mInflater服务通过inflate方法返回一个View控件对象
通过自己的布局文件转化的View对象,转化之后就可以在bindView处理
先new出当前的布局,new出每一个条目的布局文件。
@Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return mInflater.inflate(R.layout.list_item_user, parent, false); }
//设置值 tv_name.setText(cursor.getString(cursor.getColumnIndex("username"))); tv_phone.setText(cursor.getString(cursor .getColumnIndex("userphone")));
cursor.getString():cursor对应列的值
当你关闭activity时有返回值的情况下用startActivityForResult方法,并通过重新onActivityResult方法处理
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // 请求码 if (requestCode == requestUpdCode) { // 结果码 if (resultCode == 2) { // 传过来的数据 if (data.getIntExtra("count", 0) > 0) { initData();// 重新加载界面 Toast.makeText(this, "更新成功", 0).show(); } else { Toast.makeText(this, "更新失败", 0).show(); } } } }
给控件设置intent传过来的值 是数字都必须加“”转为字符串
<span style="white-space:pre"> </span>// 获取意图对象 Intent intent = getIntent(); // 给控件设置intent传过来的值 是数字都必须加“”转为字符串 etId.setText(intent.getIntExtra("uid", 0) + ""); etName.setText(intent.getStringExtra("uname")); etAge.setText(intent.getIntExtra("uage", 18) + ""); etSalary.setText(intent.getDoubleExtra("usalary", 0) + ""); etPhone.setText(intent.getStringExtra("uphone"));
ContentValues values = new ContentValues(); values.put("userid", uid); values.put("username", uname); values.put("userage", uage); values.put("usersalary", usalart); values.put("userphone", uphone);
put的key值必须是数据库里的值
转载请标明转载地址:http://blog.csdn.net/zhaoyazhi2129/article/details/29875555