【问题标题】:how to fetch number for specific name in spinner select event如何在微调器选择事件中获取特定名称的数字
【发布时间】:2011-10-15 01:10:44
【问题描述】:

我想按名称获取联系人号码。如果我选​​择存储在一个微调器中的任何名称,名称将存储在微调器中。请检查我的方法如何按名称获取号码。

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();

        }

【问题讨论】:

    标签: android


    【解决方案1】:

    检查这段代码希望你明白

    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;
    

    【讨论】:

    • 感谢 pratik 的回复,但我想要存储在 spinner 中的特定名称的编号。我该怎么做。假设如果我想要 id 如果选择了名称,那么我的查询是什么?请帮帮我
    • 这里是完整的,如果找到的名称比它有 ID,则可以传递名称,以便将 ID 传递到另一个游标中以获取数字。获取选定的名称并将第一个查询传递给此检查
    【解决方案2】:

    如何按名称获取号码

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

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

    如果使用标准ArrayAdapter,诀窍是使该配对值对象的#toString() 方法返回的值返回所需的值以显示给用户,并且该 值将用于 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;
        }});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多