首页 文章

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

提问于
浏览
0

我想通过name.name获取联系人号码存储在微调器中如果我选择存储在一个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

    检查这段代码希望你明白这个想法

    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

    我如何按名称获取数字

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

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

    如果使用标准的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;
        }});
    

相关问题