0
votes

i want to fetch contact number by name.name is stored in spinner if i select any name that is store in one spinner.plz check my method how can i fetch number by name.

public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) {

            String[] name=new String[]{arg0.getItemAtPosition(arg2).toString()};
            //name=arg0.getItemAtPosition(arg2).toString();
            String[] projection=new String[]{People.NUMBER};
            //String[] selectionarg=new String[]{People.NAME};
            Cursor cur=getContentResolver().query(People.CONTENT_URI, projection,People.NAME+"=?" ,name, null);
            String result=cur.getString(cur.getColumnIndex(People.NUMBER));
            Toast.makeText(getApplicationContext(), "number is:-"+result, Toast.LENGTH_SHORT).show();

        }
2

2 Answers

0
votes

check this code hope you get the idea

ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,PhoneLookup.DISPLAY_NAME+"='"+sel_name+"'", null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            if(name.equals(sel_name)){
                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                            new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, 
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                            new String[]{id}, null);

                    while (pCur.moveToNext()) {
                        for(int i=0;i<pCur.getColumnCount();i++)
                            number = pCur.getString(i);

                    } 
                    pCur.close();
                    pCur = null;
                }
            }
        }
    }
    cur.close();
    cur = null;
    cr = null;
0
votes

how can i fetch number by name

There is an alternate solution where you fetch all of the data you need in the first query---both the display value, and the associated data value to use when that item is selected (here the number and the name).

If you store the two fields in an object and bind instances of that type to an ArrayAdapter that backs the spinner, you can easily look up the number corresponding with the name when an item is selected and avoid an extra database call upon selection (which will simplify your code, especially if you're properly putting DB accesses on a background thread).

If using a standard ArrayAdapter, the trick is to make the value returned by the #toString() method of that paired-value object return the desired value to display to the user, and that value will be used in the content of the Spinner's display text.

Sample pair class:

public static final class ContactSpinnerItem {
  public final String contactName;
  public final String contactNumber;

  public ContactSpinnerItem(final String contactName, final String contactNumber) {
    this.contactName = contactName;
    this.contactNumber = contactNumber;
  }

  // if used in an ArrayAdapter, this will be used as the display value
  @Override
  public String toString() { return contactName; }
}

Setting the ArrayAdapter instance on the spinner and looking up the matching string in the listener:

// you probably want to build this in a loop from your cursor result
final List<ContactSpinnerItem> selectOptions = new ArrayList<>(...);
selectOptions.add(new ContactSpinnerItem(...));

final Spinner spinner = (Spinner) ...findViewById(...);
spinner.setAdapter(new ArrayAdapter<>(
  contextInstance, R.layout.my_simple_textbox, selectOptions));

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedLisener() {
    ...
    @Override
    public void onItemSelected(..., final int position, ...) {
      // note that this is:
      //   1. updating some stateful member variable with the selected item
      //   2. looking up the object corresponding with the selected Spinner index in the list of available options to use the backing data value for the string shown to the user
      MyEnclosingActivity.this.selectedNumber = selectOptions.get(position).contactNumber;
    }});