2017-04-05 59 views
0

我试图将已存储在联系人列表中的联系人列表存储到列表视图中。到目前为止,当我点击按钮实际上并没有发生。没有运行时错误;根本没有什么反应这是在谷歌Nexus 7上运行。抓取联系人并将其显示到列表视图中

这是代码。

contactsList = (ListView) findViewById(R.id.contactsList); 
     btnContacts = (Button) findViewById((R.id.btnContacts)); 
     StoreContacts = new ArrayList<String>(); 

     // toasts the user that their contacts are now being accessed 
//  EnableRuntimePermission(); 

     btnContacts.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       GetContactsIntoArrayList(); 

       arrayAdapter = new ArrayAdapter<String>(
         DisplayInformation.this, 
         R.layout.activity_display_information, // possibly change if it doesn't work 
         R.id.contactsList, StoreContacts 
       ); 

       contactsList.setAdapter(arrayAdapter); 
       contactsList.setTextFilterEnabled(true); 
} 

    }); 


    public void GetContactsIntoArrayList() { 

     cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 

     while (cursor.moveToNext()) { 

      name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
     } 
    } 
+0

不要使用ArrayAdapter。使用带有数据库的CursorAdapter –

+0

此外,StoreContacts永远不会改变,GetContactsIntoArrayList从来没有得到任何东西 –

+0

这可能有帮助,问题使用CursorAdapter,答案使用ArrayAdapter来额外显示电话号码: http://stackoverflow.com/questions/29636406/displaying-the-phone-number-of-a-contact-in-android-contact-content-provider –

回答

0
public void GetContactsIntoArrayList() { 

    cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 

    while (cursor.moveToNext()) { 

     name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
StoreConacts.add(name); 

    } 
} 
+0

只需在代码中添加这几行即可。 –

相关问题