【问题标题】:displaying Contact and Phone numbers in a check box在复选框中显示联系人和电话号码
【发布时间】:2012-03-14 11:35:06
【问题描述】:

我尝试使用此代码在 listView 的复选框中显示电话号码:

@Override
protected void onCreate(Bundle savedInstanceState) {
   // TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main1);
   lv = (ListView) findViewById(R.id.contactList);

   Cursor numberCursor = null;
   Cursor peopleCursor = getContentResolver().query(ContactsContract
        .Contacts.CONTENT_URI,null,null
           ,null,null);  

   String [] nb = null ;
   String [] Tname = null;

   if(peopleCursor.getCount()>0)
   {  
       peopleCursor.moveToFirst();

       for(int i=0;i<peopleCursor.getCount();i++)
       {  
           //get number
           numberCursor=getContentResolver().query(ContactsContract
                .CommonDataKinds.Phone.CONTENT_URI, 
                new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}
                ,ContactsContract.CommonDataKinds.Phone._ID+"="+peopleCursor
                .getString(peopleCursor.getColumnIndex(ContactsContract.Contacts._ID)),  
                null,null);
           numberCursor.moveToFirst(); 

           String number=numberCursor.getString(numberCursor 
                  .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
           nb[i]=number;

           //get name
           String name=peopleCursor.getString(numberCursor
               .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
           Tname[i]=name;

           peopleCursor.moveToNext();                   
       }   

       SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,  
           R.layout.contact_entry, numberCursor,nb, 
           new int[] {R.id.checkBox});

           lv.setAdapter(adapter);

    }

} 

但我在 logcat 中收到此错误,我不明白 --->

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0  

【问题讨论】:

    标签: android listview checkbox contactscontract


    【解决方案1】:

    首先,我认为你应该在使用它们之前定义 nb 和 Tname 的大小:

          String[] Tname  = new String[peopleCursor.getCount()];
    
          String[] nb = new String[numberCursor.getCount()];
    

    但我认为问题在于您的联系人列表中的每个人可能拥有一个或多个电话号码。 所以,peopleCursornumberCursor 应该有两个循环。并且您的索引(代码中的i)应仅用于数组Tname

    【讨论】:

    • 是的,我定义了 nb 和 Tname 的大小,但总是相同的错误---->“android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0”
    猜你喜欢
    • 2016-01-08
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    相关资源
    最近更新 更多