如何在微调器选择事件中获取特定名称的数字

时间:2011-07-28 11:27:16

标签: android

我希望按名称获取联系人号码。如果我选​​择存储在一个spinner.plz中的任何名称,则存储在微调器中。检查我的方法如何按名称获取数字。

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 个答案:

答案 0 :(得分:0)

检查此代码,希望您明白

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;

答案 1 :(得分:0)

  

如何按名称获取号码

有一个替代解决方案,您可以在第一个查询中获取所需的所有数据 - 显示值和选择该项目时使用的相关数据值(此处为数字和名称)。

如果将两个字段存储在一个对象中,并将类型的实例绑定到一个支持微调器的 ArrayAdapter ,则可以轻松查找与该对象相对应的数字。选择项目时的名称在选择时避免额外的数据库调用(这将简化您的代码,特别是如果您正确地将DB访问放在后台线程上)。

如果使用标准 ArrayAdapter ,诀窍是使该配对值对象的#toString()方法返回的值返回所需的值以显示给用户,并且< em> 值将用于Spinner显示文本的内容。

样本对类:

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; }
}

在微调器上设置 ArrayAdapter 实例,并在侦听器中查找匹配的字符串:

// 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;
    }});