編輯:關於Android編程
通過ContentResolver操作內容提供者的數據,將姓名,電話顯示在listView中,並帶有添加和刪除按鈕進行相應操作
activity_user_update.xml
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(); } }
// 構造器 public MyCursorAapter(Context context, Cursor c, int flags) { super(context, c, flags); this.context = context; mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); }
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傳過來的值 是數字都必須加“”轉為字符串
// 獲取意圖對象 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"));
<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;"> 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
說到圓角濾鏡(效果)很多人會想到app的圖標,沒錯,就是圖標。圓角化的圖片用來做圖標很美觀,這是事實。國人喜愛的iPhone的應用圖標采用的就是圓角化,很多Android
首先來看效果: 一、實現原理在實現過程中,主要考慮整個界面由若干個字母組成的子母線條組成,這樣的話把固定數量的字母封裝成一個字母線條,而每個字母又封裝成一個對象,這樣的話
安卓消息處理類:Looper、Handler、MessageQueue、Message、ThreadLocal、ThreadLocal.Values、HandlerThr
堅果手機相信受到了很多朋友的關注,說不定就已經有不少朋友入手了。我們手機在滑動窗口的時候因為設置不同可能會導致窗口動畫縮放的速度不同,大家就就會覺得自己的手