Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android CursorAdapter,搜索字符串为联系人姓名或号码_Android_Search_Android Contacts_Autocompletetextview_Android Cursoradapter - Fatal编程技术网

Android CursorAdapter,搜索字符串为联系人姓名或号码

Android CursorAdapter,搜索字符串为联系人姓名或号码,android,search,android-contacts,autocompletetextview,android-cursoradapter,Android,Search,Android Contacts,Autocompletetextview,Android Cursoradapter,嗨,我正在开发一个应用程序来搜索联系人。搜索字符串可以是数字或联系人姓名。我可以根据联系人姓名搜索列表。如果用户输入联系人号码,则不确定如何搜索该联系人。这是我正在使用的代码 Cursor contacts = getContacts2(null); startManagingCursor(contacts); TestForCursorAdapter adapter = new TestForCursorAdapter(this, contact

嗨,我正在开发一个应用程序来搜索联系人。搜索字符串可以是数字或联系人姓名。我可以根据联系人姓名搜索列表。如果用户输入联系人号码,则不确定如何搜索该联系人。这是我正在使用的代码

      Cursor contacts = getContacts2(null);
        startManagingCursor(contacts);


        TestForCursorAdapter adapter = new TestForCursorAdapter(this, contacts);
        mTxtPhoneNo.setAdapter(adapter);
        mTxtPhoneNo.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Cursor cursor = (Cursor) arg0.getItemAtPosition(arg2);
                String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                mTxtPhoneNo.setText(number);
            }
        });



    public Cursor getContacts2(String where)
    {
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone._ID,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.TYPE,
                ContactsContract.CommonDataKinds.Phone.NUMBER};

        Cursor people = this.getContentResolver().query(uri, projection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

        return people;
    }


    public class TestForCursorAdapter extends CursorAdapter implements Filterable
       {
    ImageView imageAvatar;
    ContentResolver contentResolver;
    LayoutInflater layoutInflater;
    ArrayList<String> arrofnumberfrominbuiltsms = new ArrayList<String>();
    ArrayList<String> arrofnumbettocomapreandbold = new ArrayList<String>();
    String recipient_ids;
    String r_address;
    String contactName;
    String contactID;

    String bold= "false";
    private String snippet;
    private long timestamp;
    private String dateString;
    private String timeString;
    String threadid;
    private String phoneNumber;
    private String numberType;

    private ContentResolver mContext;

    @SuppressWarnings("deprecation")
    public TestForCursorAdapter(Context context, Cursor people)
    {
        super(context, people);
        layoutInflater = LayoutInflater.from(context);
        mContext = context.getContentResolver();
    }

    @Override
    public View newView(Context context, Cursor people, ViewGroup parent)
    {
        final LayoutInflater mInflater = LayoutInflater.from(context);
        final View ret = mInflater.inflate(R.layout.custcontview, null);
        return ret;
    }

    @SuppressLint("SimpleDateFormat")
    @Override
    public void bindView(View vi, Context context, Cursor cursor) 
    {
        TextView cname = (TextView)vi.findViewById(R.id.ccontName);
        TextView cnumber = (TextView)vi.findViewById(R.id.ccontNo);
        TextView ctype = (TextView)vi.findViewById(R.id.ccontType);

        int nameIdx = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
        int numberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

        String name = cursor.getString(nameIdx);
        int type = cursor.getInt(typeIdx);
        String number = cursor.getString(numberIdx);

        cname.setText(name);
        if (type == 1) 
        {
            ctype.setText("Home");
        }
        else if (type == 2)
        {
            ctype.setText("Mobile");
        }
        else if (type == 3) 
        {
            ctype.setText("Work");
        }
        else 
        {
            ctype.setText("Other");
        }
        cnumber.setText(number); 
    }

    @Override
    public String convertToString(Cursor cursor) 
    {
        int nameCol = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        String name = cursor.getString(nameCol);
        return name;

    }

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) 
    {
        // this is how you query for suggestions
        // notice it is just a StringBuilder building the WHERE clause of a cursor which is the used to query for results
        if (getFilterQueryProvider() != null) 
        { 
            return getFilterQueryProvider().runQuery(constraint); 
        }

        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone._ID,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.TYPE,
                ContactsContract.CommonDataKinds.Phone.NUMBER};

        if (( constraint).toString().matches("[0-9]+") && constraint.length() > 2) 
        {//This is what I want to try
            return mContext.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, 
                    "UPPER(" + ContactsContract.CommonDataKinds.Phone.NUMBER + ") LIKE '" + constraint.toString().toUpperCase() + "%'", null, 
                    ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
        }
        else
        {
            return mContext.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, 
                    "UPPER(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") LIKE '" + constraint.toString().toUpperCase() + "%'", null, 
                    ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
        }
    }
}
请帮忙。谢谢

您只需要使用

Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, searchString);

Android在查找URL方面做得很好,像这样

替代了您使用的其他URI我应该在runQueryOnBackgroundThread中使用这行代码吗?这解决了问题吗?你能接受这个答案来结束这个问题并把它从未回答的列表中去掉吗,谢谢