【问题标题】:Getting details of only those contacts whose birthday exists in the contact database (Android)仅获取联系人数据库中存在生日的联系人的详细信息(Android)
【发布时间】:2013-11-27 09:06:53
【问题描述】:

我正在尝试查询联系人提供者并获取联系人信息,到目前为止我已成功列出此信息,但是我更感兴趣的是检索那些指定生日的联系人的信息。到目前为止,我的代码如下所示:

    public class LoaderClass extends Activity implements LoaderCallbacks<Cursor>{

    // private ProgressDialog pDialog;
    // Async as;
    TextView t1, t2;
    Locale current; 
    Uri uri;
    String[] projection;
    String where;

    public static final int progress_bar_type = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loader_class);

        t1 = (TextView) findViewById(R.id.textView1);
        t2 = (TextView) findViewById(R.id.textView2);
        t1.setText("");
        current = getResources().getConfiguration().locale;
        // new Async().execute();

        uri = ContactsContract.Data.CONTENT_URI;

        projection = new String[] {
                // take from the contacts
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Event.CONTACT_ID,
                ContactsContract.CommonDataKinds.Event.START_DATE,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
                ContactsContract.CommonDataKinds.Photo.PHOTO


        };

         where = ContactsContract.Data.MIMETYPE + "= ? AND "
                + ContactsContract.CommonDataKinds.Event.TYPE + "="
                + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;


        String[] selectionArgs = new String[] { ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };

        getLoaderManager().initLoader(0, null, this);


    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        CursorLoader loader = new CursorLoader(
                this,
                uri, 
                projection, 
                null, 
                null, 
                null);
        return loader;
    }

    @Override
    public void onLoadFinished(
            Loader<Cursor> loader, 
            Cursor cursor) {


        while (cursor.moveToNext()) {

            String displayBirthday = cursor
                    .getString(cursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));

            String name = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


            String ename = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));


            t1.append("\n"+displayBirthday+"\n"+name+"\n"+ename);
        }


    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
        // TODO Auto-generated method stub

    }

}

我想设计一个 where 子句,但我不明白怎么做。

【问题讨论】:

    标签: android android-contacts


    【解决方案1】:

    请使用以下查询:

    String[] projectionFields = new String[]{
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
    };
    ArrayList<Contact> listContacts = new ArrayList<>();
    CursorLoader cursorLoader = new CursorLoader(context,
            ContactsContract.Contacts.CONTENT_URI,
            projectionFields, // the columns to retrieve
            null, // the selection criteria (none)
            null, // the selection args (none)
            null // the sort order (default)
    );
    
    Cursor c = cursorLoader.loadInBackground();
    
    final Map<String, SynchData.clientsData> contactsMap = new HashMap<>(c.getCount());
    
    if(c.moveToFirst()){
    
        int idIndex = c.getColumnIndex(ContactsContract.Contacts._ID);
        int nameIndex = c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
    
        do {
            String contactId = c.getString(idIndex);
    
            String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ? AND " +
                    ContactsContract.CommonDataKinds.Event.TYPE + " = " + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
            String[] selectionArgs = new String[]{id, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};
    
            Cursor birthDateCur = cr.query(ContactsContract.Data.CONTENT_URI, null, where, selectionArgs, null);
    
            while (birthDateCur.moveToNext()) {
                birthDate = birthDateCur.getString(birthDateCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
                if (birthDate != null && !birthDate.equalsIgnoreCase("")) {
                    if (!birthDate.equalsIgnoreCase("")) {
                        print(" birthDate " + birthDate);
                        break;
                    }
                }
            }
            birthDateCur.close();
    
    
        } while (c.moveToNext());
    }
    c.close();
    

    【讨论】:

      【解决方案2】:
      // method to get name, contact id, and birthday
      private Cursor getContactsBirthdays() {
      Uri uri = ContactsContract.Data.CONTENT_URI;
      
      String[] projection = new String[] {
              ContactsContract.Contacts.DISPLAY_NAME,
              ContactsContract.CommonDataKinds.Event.CONTACT_ID,
              ContactsContract.CommonDataKinds.Event.START_DATE
      };
      
      String where =
              ContactsContract.Data.MIMETYPE + "= ? AND " +
              ContactsContract.CommonDataKinds.Event.TYPE + "=" + 
              ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
      String[] selectionArgs = new String[] { 
          ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE
      };
      String sortOrder = null;
      return managedQuery(uri, projection, where, selectionArgs, sortOrder);
      }
      
      // iterate through all Contact's Birthdays and print in log
      Cursor cursor = getContactsBirthdays();
         int bDayColumn =      cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE);
        while (cursor.moveToNext()) {
      String bDay = cursor.getString(bDayColumn);
      Log.d(TAG, "Birthday: " + bDay);
      }
      

      【讨论】:

      • 伙伴 我是在同一个教程中完成的,但是如果我使用 where 子句,我会丢失其他字段的信息。我需要一个更健壮的 where 子句或一个如何制作的指针..
      【解决方案3】:

      制作 where 子句:

          String where =
          ContactsContract.Data.MIMETYPE + "= ? AND " +
          ContactsContract.CommonDataKinds.Event.TYPE + "=" + 
          ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
      

      并替换您的以下行:

      @SuppressWarnings("deprecation")
          Cursor cursor = managedQuery(uri, projection, null, null,
                  sortOrder);
      

      有了这个

      Cursor cursor = managedQuery(uri, projection, where, null,
                  sortOrder);
      

      【讨论】:

      • 你能解释一下 where 子句的作用吗?
      • 我使用了教程中指定的上述查询,但是它确实返回了生日的联系人,但无法加载我感兴趣的其他信息。无论如何我会再试一次。
      【解决方案4】:

      不要惊慌,只需从所有联系人中列出一份清单。第 2 步:现在我认为您获取所有联系信息,第 3 步:将其保存在一个包装类中,然后:

      ArrayList<String> matchname = new ArrayList<String>();
                      for(int k =0;k<yourcontactwrapperlist.size();k++)
                      {
      
                              String keyname = yourcontactwrapperlist.get(i).yourgettermethodnameforbirthday();
      
                              if((keyname.trim().size()<0))
                              {
                                  matchname.add(keyname);
      
                              }
                          }
      }
      

      然后在临时列表中你会得到你想要显示的所有数据。

      【讨论】:

      • 不,我不想保存在列表中然后查找,这将是双倍的努力,这会使我的处理速度变慢,而带有 where 子句的查询将为我完成。跨度>
      • “哦”是什么意思?
      猜你喜欢
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多