Android ContentProvider读取并显示联系人信息

做的比较急,来不及做功课就简单贴下代码。往后再补充…


main.xml




    

    

        

        

            
        

    

    

JavaCode

package com.example.contentprovider;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.FilterQueryProvider;
import android.widget.Filterable;
import android.widget.TextView;



/**
 * autoCompleteTextView自动文本框的适配器
 */
public class ContactListAdapter extends CursorAdapter implements Filterable {
    private ContentResolver resolver;
    private String[] columns = new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME};

    public ContactListAdapter(Context context, Cursor c) {
        // 调用父类构造方法
        super(context, c);
        resolver = context.getContentResolver();    // 初始化ContentResolver
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        TextView view = (TextView) inflater.inflate(android.R.layout.simple_dropdown_item_1line, parent, false);
        view.setText(cursor.getString(1));
        return view;
    }

    // 形成一个下拉菜单
    @Override
    public void bindView(View arg0, Context arg1, Cursor arg2) {
        ((TextView) arg0).setText(arg2.getString(1));
    }

    @Override
    public CharSequence convertToString(Cursor cursor) {
        return cursor.getString(1);
    }

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        FilterQueryProvider filter = getFilterQueryProvider();
        if (filter != null) {
            return filter.runQuery(constraint);
        }
        Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, Uri.encode(constraint.toString()));
        return resolver.query(uri, columns, null, null, null);
    }
}
package com.example.contentprovider;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements TextWatcher {
    private AutoCompleteTextView textView;
    private TextView textView2;
    private String[] columns = new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, columns, null, null, null);
        ContactListAdapter adapter = new ContactListAdapter(this, cursor);
        textView = findViewById(R.id.edit);
        textView2 = findViewById(R.id.textView2);
        textView.setAdapter(adapter);
        textView.addTextChangedListener(this);
    }

    // 根据联系人的姓名查询电话
    private String getQueryData(String name) {
        String phoneNum = "";
        ContentResolver contentResolver1 = getContentResolver();
        String[] colmuns1 = new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME};
        Cursor cursor1 = contentResolver1.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);
        for (cursor1.moveToFirst(); !(cursor1.isAfterLast()); cursor1
                .moveToNext()) {
            String contactsName = cursor1.getString(cursor1
                    .getColumnIndex(colmuns1[1]));
            if (name.equals(contactsName)) {
                int id = cursor1.getInt(cursor1.getColumnIndex(colmuns1[0]));
                ContentResolver contentResolver2 = getContentResolver();
                String[] colmuns2 = new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                        ContactsContract.CommonDataKinds.Phone.NUMBER};
               
                Cursor cursor2 = contentResolver2.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null, colmuns2[0] + "=" + id, null, null);
                if (cursor2.moveToNext()) {
                    phoneNum = cursor2.getString(cursor2
                            .getColumnIndex(colmuns2[1]));

                }
            }
        }
        return phoneNum;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String name = textView.getText() + "";
        String phoneNum = getQueryData(name);
        if (phoneNum.equals("")){
            textView2.setText("无此联系人");
        }else {
            textView2.setText("电话为:" + phoneNum);
        }
    }
}

manifest.xml

获取权限

    

你可能感兴趣的:(Android学习)