仅显示基于从Parse检索的电话号码列表的联系人

时间:2015-12-31 17:18:22

标签: android parse-platform

我正在尝试获取Parse用户列表,然后将其与用户手机中的联系人进行比较,然后仅显示在Parse后端有编号的联系人,但是我现在的代码不能正常工作期待它并且我相信它与Parse返回的List有关(也许我需要转换它因为我占用了整个Object而我只需要一个字段(电话号码在我的解析后端是= username) 。

以下是获取Parse列表的代码:

// Get parse users
        String [] phoneNumberList = {"DUMMY NUMBER1", "DUMMY NUMBER2"};
        ParseQuery<ParseUser> query = ParseUser.getQuery(); {
            query.whereContainedIn("username", Arrays.asList(phoneNumberList));
            query.findInBackground(new FindCallback<ParseUser>() {
                public void done (List<ParseUser> list, ParseException e){
                    if (e == null) {
                        mBinderPhoneNumbers = list;
                        Log.d("TAG", "mBinderPhoneNumbers from onCreate: " + mBinderPhoneNumbers);
                        addContactsInList();
                    } else {
                        // Something went wrong.
                        Log.e("TAG", "User query error: " + e);
                    }
                }
            });
        }



这是addContactsInList代码,我尝试使用该列表仅加载Parse后端中的联系人:

private void addContactsInList() {
        // TODO Auto-generated method stub


        Thread thread = new Thread() {
            @Override
            public void run() {

                showPB();

                try {

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

                    try {
                        ContactsListClass.phoneList.clear();
                    } catch (Exception e) {

                    }


                    while (phones.moveToNext()) {


                        Log.d("TAG", "mBinderPhoneNumbers from AddContactsInList:" + mBinderPhoneNumbers);
                        if (Arrays.asList(mBinderPhoneNumbers).contains(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER)))) {
                            String phoneName = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER));
                            String phoneImage = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                            ContactObject cp = new ContactObject();
                            cp.setName(phoneName);
                            cp.setNumber(phoneNumber);
                            cp.setImage(phoneImage);
                            ContactsListClass.phoneList.add(cp);
                        }
                    }
                    phones.close();
                    lv = new ListView(context);
                    lv.setLayoutParams(new LayoutParams(
                            LayoutParams.MATCH_PARENT,
                            LayoutParams.MATCH_PARENT));

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            llContainer.addView(lv);
                        }
                    });

                    Collections.sort(ContactsListClass.phoneList,
                            new Comparator<ContactObject>() {
                                @Override
                                public int compare(ContactObject lhs,
                                                   ContactObject rhs) {
                                    return lhs.getName().compareTo(
                                            rhs.getName());
                                }
                            });

                    objAdapter = new ContactsAdapter(AddContactsActivity.this,
                            ContactsListClass.phoneList);
                    lv.setAdapter(objAdapter);
                    lv.setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent,
                                                View view, int position, long id) {

                            CheckBox chk = (CheckBox) view
                                    .findViewById(R.id.contactcheck);
                            ContactObject bean = ContactsListClass.phoneList
                                    .get(position);
                            if (bean.isSelected()) {
                                bean.setSelected(false);
                                chk.setChecked(false);
                            } else {
                                bean.setSelected(true);
                                chk.setChecked(true);
                            }

                        }
                    });

                } catch (Exception e) {

                    e.printStackTrace();

                }

                hidePB();

            }
        };

        thread.start();

    }

1 个答案:

答案 0 :(得分:0)

所以我设法自己解决了这个问题。我通过首先从Parse获取列表来修改我的代码,以显示仅包含用户名的数组。

ParseQuery<ParseUser> query = ParseUser.getQuery(); {
    query.whereContainedIn("username", Arrays.asList(phoneNumberList));
    query.findInBackground(new FindCallback<ParseUser>() {
        public void done (List<ParseUser> list, ParseException e){
            if (e == null) {
                mBinderPhoneNumbers = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    mBinderPhoneNumbers.add(list.get(i).getUsername());
                    addContactsInList();
                }
            } else {
                // Something went wrong.
                Log.e("TAG", "User query error: " + e);
            }
        }
    });
}



然后我修改了以下代码行并删除了代码的Arrays.asList()部分,因为我已经处理了一个ArrayList:

    while (phones.moveToNext()) {
                        if (mBinderPhoneNumbers.contains(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER)))) {
                            String phoneName = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER));


String phoneImage = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                        ContactObject cp = new ContactObject();
                        cp.setName(phoneName);
                        cp.setNumber(phoneNumber);
                        cp.setImage(phoneImage);
                        ContactsListClass.phoneList.add(cp);
                    }
                }