{"id":2133,"date":"2023-06-30T12:04:25","date_gmt":"2023-06-30T04:04:25","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=2133"},"modified":"2023-06-30T12:04:25","modified_gmt":"2023-06-30T04:04:25","slug":"android%e9%80%9a%e8%ae%af%e5%bd%95%e5%bc%80%e5%8f%91%e5%ae%8c%e6%95%b4%e4%bb%a3%e7%a0%81%e7%bb%86%e8%8a%82%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2023\/2133\/","title":{"rendered":"android\u901a\u8baf\u5f55\u5f00\u53d1\u5b8c\u6574\u4ee3\u7801\u7ec6\u8282\u4ecb\u7ecd"},"content":{"rendered":"

Android\u901a\u8baf\u5f55\u662f\u4e00\u6b3e\u7528\u4e8e\u7ba1\u7406\u8054\u7cfb\u4eba\u4fe1\u606f\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u4e3b\u8981\u529f\u80fd\u5305\u62ec\u65b0\u589e\u3001\u7f16\u8f91\u3001\u5220\u9664\u8054\u7cfb\u4eba\uff0c\u4ee5\u53ca\u641c\u7d22\u8054\u7cfb\u4eba\u7b49\u3002\u672c\u6587\u5c06\u4ecb\u7ecdAndroid\u901a\u8baf\u5f55\u7684\u5f00\u53d1\u539f\u7406\u53ca\u5b8c\u6574\u4ee3\u7801\u3002<\/p>\n

1.\u5f00\u53d1\u539f\u7406<\/p>\n

Android\u901a\u8baf\u5f55\u7684\u5f00\u53d1\u4e3b\u8981\u6d89\u53ca\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n

1\uff09\u8054\u7cfb\u4eba\u6570\u636e\u7684\u5b58\u50a8\uff1aAndroid\u901a\u8baf\u5f55\u7684\u8054\u7cfb\u4eba\u6570\u636e\u5b58\u50a8\u5728\u7cfb\u7edf\u63d0\u4f9b\u7684Content Provider\u4e2d\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7Content Provider\u63d0\u4f9b\u7684API\u8fdb\u884c\u6570\u636e\u7684\u64cd\u4f5c\u3002<\/p>\n

2\uff09\u8054\u7cfb\u4eba\u6570\u636e\u7684\u5c55\u793a\uff1a\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7ListView\u6216RecyclerView\u7b49\u63a7\u4ef6\u5c55\u793a\u8054\u7cfb\u4eba\u6570\u636e\uff0c\u5e76\u901a\u8fc7Adapter\u5c06\u6570\u636e\u4e0e\u63a7\u4ef6\u8fdb\u884c\u7ed1\u5b9a\u3002<\/p>\n

3\uff09\u8054\u7cfb\u4eba\u6570\u636e\u7684\u64cd\u4f5c\uff1a\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u5bf9ListView\u6216RecyclerView\u7684Item\u8fdb\u884c\u70b9\u51fb\u4e8b\u4ef6\u76d1\u542c\uff0c\u5b9e\u73b0\u5bf9\u8054\u7cfb\u4eba\u6570\u636e\u7684\u64cd\u4f5c\uff0c\u5305\u62ec\u65b0\u589e\u3001\u7f16\u8f91\u3001\u5220\u9664\u7b49\u3002<\/p>\n

2.\u5b8c\u6574\u4ee3\u7801<\/p>\n

\u4e0b\u9762\u662fAndroid\u901a\u8baf\u5f55\u7684\u5b8c\u6574\u4ee3\u7801\uff0c\u5305\u62ec\u8054\u7cfb\u4eba\u6570\u636e\u7684\u5c55\u793a\u548c\u64cd\u4f5c\uff1a<\/p>\n

1\uff09\u5e03\u5c40\u6587\u4ef6activity_main.xml<\/p>\n

“`<\/p>\n<\/p>\n

android:layout_width=”match_parent”<\/p>\n

android:layout_height=”match_parent”><\/p>\n<\/p>\n

android:id=”@+id\/listView”<\/p>\n

android:layout_width=”match_parent”<\/p>\n

android:layout_height=”match_parent” \/><\/p>\n<\/p>\n

android:id=”@+id\/addBtn”<\/p>\n

android:layout_width=”wrap_content”<\/p>\n

android:layout_height=”wrap_content”<\/p>\n

android:text=”\u65b0\u589e”<\/p>\n

android:layout_alignParentBottom=”true”<\/p>\n

android:layout_alignParentRight=”true”<\/p>\n

android:layout_marginRight=”20dp”<\/p>\n

android:layout_marginBottom=”20dp” \/><\/p>\n

“`<\/p>\n

2\uff09\u8054\u7cfb\u4eba\u6570\u636e\u7684\u5c55\u793aMainActivity.java<\/p>\n

“`<\/p>\n

publ<\/p>\n

<\/figure>\n<\/p>\n

ic class MainActivity extends AppCompatActivity {<\/p>\n

private ListView mListView;<\/p>\n

private Button mAddBtn;<\/p>\n

private List mContactList;<\/p>\n

@Override<\/p>\n

protected void onCreate(Bundle savedInstanceState) {<\/p>\n

super.onCreate(savedInstanceState);<\/p>\n

setContentView(R.layout.activity_main);<\/p>\n

mListView = findViewById(R.id.listView);<\/p>\n

mAddBtn = findViewById(R.id.addBtn);<\/p>\n

mContactList = new ArrayList();<\/p>\n

\/\/\u83b7\u53d6\u8054\u7cfb\u4eba\u6570\u636e<\/p>\n

getContacts();<\/p>\n

\/\/\u8bbe\u7f6eListView\u7684Adapter<\/p>\n

mListView.setAdapter(new ContactAdapter(this, mContactList));<\/p>\n

\/\/\u65b0\u589e\u8054\u7cfb\u4eba<\/p>\n

mAddBtn.setOnClickListener(new View.OnClickListener() {<\/p>\n

@Override<\/p>\n

public void onClick(View v) {<\/p>\n

Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);<\/p>\n

\u82f9\u679c\u514d\u7b7eAPP\u7f51\u5740\u5728\u54ea\u91cc\u627e\u7684<\/a> startActivity(intent);<\/p>\n

}<\/p>\n

});<\/p>\n

}<\/p>\n

\/\/\u83b7\u53d6\u8054\u7cfb\u4eba\u6570\u636e<\/p>\n

private void getContacts() {<\/p>\n

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);<\/p>\n

if (cursor != null) {<\/p>\n

while (cursor.moveToNext()) {<\/p>\n

Contact contact = new Contact();<\/p>\n

String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));<\/p>\n

String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));<\/p>\n

contact.setId(id);<\/p>\n

contact.setName(name);<\/p>\n

mContactList.add(contact);<\/p>\n

}<\/p>\n

cursor.close();<\/p>\n

}<\/p>\n

}<\/p>\n

}<\/p>\n

“`<\/p>\n

3\uff09\u8054\u7cfb\u4eba\u6570\u636e\u7684\u64cd\u4f5cContactAdapter.java<\/p>\n

“`<\/p>\n

public class ContactAdapter extends BaseAdapter {<\/p>\n

private Context mContext;<\/p>\n

private List mContactList;<\/p>\n

public ContactAdapter(Context context, List contactList) {<\/p>\n

mContext = context;<\/p>\n

mContactList = contactList;<\/p>\n

}<\/p>\n

@Override<\/p>\n

public int getCount() {<\/p>\n

return mContactList.size();<\/p>\n

}<\/p>\n

@Override<\/p>\n

public Object getItem(int position) {<\/p>\n

return mContactList.get(position);<\/p>\n

}<\/p>\n

@Override<\/p>\n

public long getItemId(int position) {<\/p>\n

return position;<\/p>\n

}<\/p>\n

@Override<\/p>\n

public View getView(final int position, View convertView, ViewGroup parent) {<\/p>\n

ViewHolder holder;<\/p>\n

if (convertView == null) {<\/p>\n

convertView = LayoutInflater.from(mContext).inflate(R.layout.item_contact, parent, false);<\/p>\n

holder = new ViewHolder();<\/p>\n

holder.nameTv = convertView.findViewById(R.id.nameTv);<\/p>\n

holder.editBtn = convertView.findViewById(R.id.editBtn);<\/p>\n

holder.deleteBtn = convertView.findViewById(R.id.deleteBtn);<\/p>\n

convertView.setTag(holder);<\/p>\n

} else {<\/p>\n

holder = (ViewHolder) convertView.getTag();<\/p>\n

}<\/p>\n

final Contact contact = mContactList.get(position);<\/p>\n

holder.nameTv.setText(contact.getName());<\/p>\n

\/\/\u7f16\u8f91\u8054\u7cfb\u4eba<\/p>\n

holder.editBtn.setOnClickListener(new View.OnClickListener() {<\/p>\n

@Override<\/p>\n

public void onClick(View v) {<\/p>\n

Intent intent = new Intent(Intent.ACTION_EDIT);<\/p>\n

intent.setData(Uri.parse(ContactsContract.Contacts.CONTENT_LOOKUP_URI + “\/” + contact.getId()));<\/p>\n

mContext.startActivity(intent);<\/p>\n

}<\/p>\n

});<\/p>\n

\/\/\u5220\u9664\u8054\u7cfb\u4eba<\/p>\n

holder.deleteBtn.setOnClickListener(new View.OnClickListener() {<\/p>\n

@Override<\/p>\n

public void onClick(View v) {<\/p>\n

Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contact.getId());<\/p>\n

mContext.getContentResolver().delete(uri, null, null);<\/p>\n

mContactList.remove(position);<\/p>\n

notifyDataSetChanged();<\/p>\n

\u5c01\u88c5app\u5e26\u626b<\/a> }<\/p>\n

});<\/p>\n

return convertView;<\/p>\n

}<\/p>\n

private static class ViewHolder {<\/p>\n

TextView nameTv;<\/p>\n

Button editBtn;<\/p>\n

Button deleteBtn;<\/p>\n

}<\/p>\n

}<\/p>\n

“`<\/p>\n

4\uff09\u8054\u7cfb\u4eba\u6570\u636e\u7684\u5b9e\u4f53\u7c7bContact.java<\/p>\n

“`<\/p>\n

public class Contact {<\/p>\n

private String id;<\/p>\n

private String name;<\/p>\n

public String getId() {<\/p>\n

return id;<\/p>\n

}<\/p>\n

public void setId(String id) {<\/p>\n

this.id = id;<\/p>\n

}<\/p>\n

public String getName() {<\/p>\n

return name;<\/p>\n

}<\/p>\n

public void setName(String name) {<\/p>\n

this.name = name;<\/p>\n

}<\/p>\n

}<\/p>\n

“`<\/p>\n

\u4ee5\u4e0a\u5c31\u662fAndroid\u901a\u8baf\u5f55\u7684\u5b8c\u6574\u4ee3\u7801\uff0c\u901a\u8fc7\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u4e86\u89e3\u5230Android\u901a\u8baf\u5f55\u7684\u5f00\u53d1\u539f\u7406\u53ca\u76f8\u5173\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Android\u901a\u8baf\u5f55\u662f\u4e00\u6b3e\u7528\u4e8e\u7ba1\u7406\u8054\u7cfb\u4eba\u4fe1\u606f\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u4e3b\u8981\u529f\u80fd\u5305\u62ec\u65b0\u589e\u3001\u7f16\u8f91\u3001\u5220\u9664\u8054\u7cfb\u4eba\uff0c\u4ee5\u53ca\u641c\u7d22\u8054\u7cfb\u4eba\u7b49\u3002\u672c\u6587\u5c06\u4ecb\u7ecdAndroid\u901a\u8baf\u5f55\u7684\u5f00\u53d1\u539f\u7406\u53ca\u5b8c\u6574\u4ee3\u7801\u30021.\u5f00\u53d1\u539f\u7406Android\u901a\u8baf\u5f55\u7684\u5f00<\/p>\n","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4481,95,4480,4479,4482],"class_list":["post-2133","post","type-post","status-publish","format-standard","hentry","category-zuoapp","tag-app","tag-95","tag--ios-ipa","tag-4479","tag-4482"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/2133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=2133"}],"version-history":[{"count":0,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/2133\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=2133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=2133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=2133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}