【问题标题】:how to show the contacts stored in android mobile while typing like the messaging screen如何在键入消息屏幕时显示存储在 android 手机中的联系人
【发布时间】:2012-07-19 00:13:10
【问题描述】:

我想在EditText 框中键入文本时显示存储在 android 手机中的联系人。我想在EditText 框下方显示联系人,就像消息屏幕一样。我可以使用ContactsContract 检索联系人,但我不知道如何像 Android 中的消息屏幕那样显示。有人知道吗?

我当前的代码:

 Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                    null, null, null, null);
 if (cur.getCount() > 0) {
    while (cur.moveToNext()) {
        String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
        System.out.println("contactsID-->>>"+id);
        String name = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        System.out.println("contactsName-->>>"+name);

        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor pCur = cr.query(Contacts.Phones.CONTENT_URI, null, 
                                   Contacts.Phones.PERSON_ID +" = ?", 
                                   new String[]{id}, null);
            int i=0;
            int pCount = pCur.getCount();    

            while (pCur.moveToNext()) {
                String phoneNum = pCur.getString(
                                       pCur.getColumnIndex(Contacts.Phones.NUMBER));
                System.out.println("PhoneNum-->>>"+phoneNum);
            }

            // Query phone here.  Covered next
        }
    }
}

【问题讨论】:

    标签: android android-contacts


    【解决方案1】:

    您必须使用AutoCompleteTextView 并用您的联系人填充它。 DOC

    【讨论】:

      【解决方案2】:

      您需要使用AutoCompleteTextView 并使用从 ContactsContract 读取的联系人姓名来填充适配器。代码:

          String CONTACTS[] = null;
          Cursor contactsCursor  = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, new String[]{Contacts.DISPLAY_NAME}, null, null, null);
          if(contactsCursor!=null){
              CONTACTS = new String[contactsCursor.getCount()];//(contactsCursor.getCount())];
              int i=0;
              while(contactsCursor.moveToNext()) {
                  CONTACTS[i] = contactsCursor.getString(contactsCursor.getColumnIndex(Contacts.DISPLAY_NAME));
                  i++;
              }
          }
          ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                  android.R.layout.simple_dropdown_item_1line, CONTACTS);
          AutoCompleteTextView textView = (AutoCompleteTextView)
                  findViewById(R.id.autocompleteTextView);
          textView.setAdapter(adapter);
      

      【讨论】:

      • 嗨,aswin,我尝试了不会在 autocompleteTextView 中显示数据的代码。
      • 我已经尝试过这段代码,它可以工作。您应该使用我提供的链接中的示例,并按照答案中所示修改代码。告诉我你的偏差在哪里,我也许可以帮助你。
      • 我检索了联系人并将其设置为适配器。但它没有显示。我想在编辑文本框中输入值时显示姓名和手机号码,就像消息屏幕一样。
      【解决方案3】:

      您可以使用自己的类来存储联系信息,根据您的要求自定义此类:-

      public class History {
          private static final int VERSION = 1;
          private static final String FILENAME = "history.dat";
      
          private Context ctx;
          private ArrayList<String> list;
      
          public History( Context ctx ) {
              this.ctx = ctx;
          }
      
          public ArrayAdapter<String> getArrayAdapter() {
              return new ArrayAdapter<String>( ctx, android.R.layout.simple_dropdown_item_1line, list );
          }
      
          public void addUrl( String url ) {
              if (!list.contains( url )) list.add( url );
          }
      
          public int size() {
              return list.size();
          }
      
          public void read() {
              list = new ArrayList<String>();
      
              try {
                  DataInputStream dis = new DataInputStream( ctx.openFileInput( FILENAME ));
      
                  dis.readInt(); // VERSION
                  int n = dis.readInt();
      
                  while (n-- > 0) list.add( dis.readUTF());
              }
              catch (IOException e) {
              }
          }
      
          public void write() {
              try {
                  DataOutputStream dos = new DataOutputStream( ctx.openFileOutput( FILENAME, 0 ));
      
                  dos.writeInt( VERSION );
                  dos.writeInt( list.size());
      
                  for (String url : list) dos.writeUTF( url );
              }
              catch (IOException e) {
              }
          }
      }
      

      在你的主课中:

      History  historyObject= new History(MainActivity.this)
      while (cursor.moveNext()) {
          historyObject.addUrl(cursor.getString(0));
      }
      

      使用自动完成文本框代替编辑文本

      autocompleteText.setAdapter(historyObject.getArrayAdapter());
      

      希望对你有帮助:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多