【问题标题】:How to check if a cursor is empty? [duplicate]如何检查游标是否为空? [复制]
【发布时间】:2013-02-07 06:41:29
【问题描述】:

当我尝试从电话的联系人列表中获取电话号码时。问题是,当我在手机中的联系人列表为空时运行应用程序时,应用程序停止了。我查了一下,这是因为光标是空的。

如何查看光标是否为空或者手机的联系人列表中是否有联系人?

ArrayList<String> lstPhoneNumber = new ArrayList<String>();
Cursor phones = getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); 
lstPhoneNumber = new ArrayList<String>();

phones.moveToFirst();
// The problematic Line:
lstPhoneNumber.add(phones.getString(phones.getColumnIndex(
        ContactsContract.CommonDataKinds.Phone.NUMBER))); 
while (phones.moveToNext()) {
    lstPhoneNumber.add(phones.getString(phones.getColumnIndex(
            ContactsContract.CommonDataKinds.Phone.NUMBER))); 
}
phones.close();

【问题讨论】:

  • 摆脱 phones.moveToFirst() 和 1stPhonenumber.add 呼叫.. 离开你的 while 循环。固定。
  • 另外,尝试传入您要查询的列的投影。当您只需要 1 时,没有用检索所有列(通过为投影传递 null)。

标签: android android-contacts android-cursor


【解决方案1】:

测试“有效”游标的一般模式是

((cursor != null) && (cursor.getCount() > 0))

Contacts Provider 不会返回 null,但其他内容提供者可能会在遇到某种数据错误时返回 null。内容提供者应该处理异常,将光标设置为零,并记录异常,但不能保证。

【讨论】:

    【解决方案2】:

    使用cursor.getCount() == 0。如果为真,则光标为空

    【讨论】:

      【解决方案3】:

      我添加了一个投影,所以你只得到你需要的列。

      String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER };
      ArrayList<String> lstPhoneNumber = new ArrayList<String>();
      Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
              projection, null, null, null);
      if (phones == null)
          return; // can't do anything with a null cursor.
      try {
          while (phones.moveToNext()) {
              lstPhoneNumber.add(phones.getString(0));
          }
      } finally {
          phones.close();
      }
      

      【讨论】:

      • 这是字母 L :)
      • 我认为手机光标不需要关闭功能。
      【解决方案4】:

      试试这个。您的代码的问题是无论光标的长度如何,它都会执行 add 。我将phones.moveToFirst() 包含在if 语句中,因为如果光标为空或没有记录集,它将返回false。

        if(phones.moveToFirst()){
             do{
                lstPhoneNumber.add(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); 
              }while(phones.moveToNext())
         } else {
              //do something else
         }
      

      【讨论】:

      • 或者只是做一个 while 循环 .. 添加额外的 if 和 do/while 看起来很丑,并增加了毫无意义的逻辑
      【解决方案5】:
      public boolean isCursorEmpty(Cursor cursor){
         return !cursor.moveToFirst() || cursor.getCount() == 0;
      }
      

      【讨论】:

        【解决方案6】:
        System.out.println("count "+ cursor.getCount());
        

        这将在logcat中显示光标的值

        【讨论】:

          【解决方案7】:
          cursor.moveToFirst();
          if (cursor.isBeforeFirst()) //means empty result set
                  ; //do your stuff when cursor is empty
          

          moveToFirst() 之后的isBeforeFirst() 也可以正常工作。

          根据Cursor documentation

          isBeforeFirst(): 返回光标是否指向第一行之前的位置。

          【讨论】:

            猜你喜欢
            • 2011-07-02
            • 2018-07-03
            • 1970-01-01
            • 1970-01-01
            • 2013-06-27
            • 2017-05-29
            • 2021-10-22
            相关资源
            最近更新 更多