如何区分我的JSON数组中的值与ListView中的其他值?

时间:2017-08-09 22:06:17

标签: android json

我有一个JSON Array,其中包含我的电话簿中的一些联系人,他们也是我的应用的用户。例如,JSON数组可能如下所示:

[{"contact_phonenumber":"11111"},{"contact_phonenumber":"22222"},{"contact_phonenumber":"33333"}]

phoneNumberofContact是一个字符串,在我的代码中的do语句中,返回手机中的每个联系人。如何查看我phoneNumberofContact中显示的JSON Array个号码,然后除了ListView中的那些联系人外,还会显示“应用用户”字样。我的ListView工作正常,我只想添加此功能。

因此,例如,对于我11111中的ListView数字:

Joe Blogs - app user
11111

这是我的代码:

JSONArray jsonArrayContacts = response;
//response is something like [{"contact_phonenumber":"11111"}, etc...]

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_contact);


        //selectPhoneContacts is an empty array list that will hold our SelectPhoneContact info
        selectPhoneContacts = new ArrayList<SelectPhoneContact>();

        listView = (ListView) findViewById(R.id.listviewPhoneContacts);

}


//******for the phone contacts in the listview

    // Load data in background
    class LoadContact extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... voids) {


//          we want to delete the old selectContacts from the listview when the Activity loads
//          because it may need to be updated and we want the user to see the updated listview,
//          like if the user adds new names and numbers to their phone contacts.
            selectPhoneContacts.clear();

//          we have this here to avoid cursor errors
            if (cursor != null) {
                cursor.moveToFirst();

            }


            try {

//                get a handle on the Content Resolver, so we can query the provider,
                cursor = getApplicationContext().getContentResolver()
//                the table to query
                 .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null,
                                null,
                                null,
//               display in ascending order
                 ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

//                get the column number of the Contact_ID column, make it an integer.
//                I think having it stored as a number makes for faster operations later on.
//                get the column number of the DISPLAY_NAME column
                int nameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
//                 get the column number of the NUMBER column
                int phoneNumberofContactIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

                cursor.moveToFirst();

//              We make a new Hashset to hold all our contact_ids, including duplicates, if they come up
                Set<String> ids = new HashSet<>();

                do {

                    System.out.println("=====>in while");

//                        get a handle on the display name, which is a string
                    name = cursor.getString(nameIdx);

//                        get a handle on the phone number, which is a string
                    phoneNumberofContact = cursor.getString(phoneNumberofContactIdx);


                    //----------------------------------------------------------


                    // get a handle on the phone number of contact, which is a string. Loop through all the phone numbers
//                  if our Hashset doesn't already contain the phone number string,
//                    then add it to the hashset
                    if (!ids.contains(phoneNumberofContact)) {
                        ids.add(phoneNumberofContact);

                                SelectPhoneContact selectContact = new SelectPhoneContact();

                                selectContact.setName(name);
                                selectContact.setPhone(phoneNumberofContact);
                                selectPhoneContacts.add(selectContact);
                            }

                } while (cursor.moveToNext());


            } catch (Exception e) {
                Toast.makeText(NewContact.this, "what the...", Toast.LENGTH_LONG).show();
                e.printStackTrace();
                //   cursor.close();
            } finally {

            }


    if (cursor != null) {
        cursor.close();

    }
    return null;
}


        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            adapter = new SelectPhoneContactAdapter(selectPhoneContacts, NewContact.this);

//                    we need to notify the listview that changes may have been made on
//                    the background thread, doInBackground, like adding or deleting contacts,
//                    and these changes need to be reflected visibly in the listview. It works
//                    in conjunction with selectContacts.clear()
            adapter.notifyDataSetChanged();

            listView.setAdapter(adapter);

       }
   }

1 个答案:

答案 0 :(得分:1)

在第一个中,您可以将jsonArrayContacts解析为List:

 final List<String> responseContacts = new ArrayList<String>();
                    try {
                        JSONArray responseObject = new JSONArray(response);
                        for (int i = 0; i < responseObject.length(); i++) {
                            final JSONObject obj = responseObject.getJSONObject(i);
                            responseContacts.add(obj.getString("contact_phonenumber"));
                        }
                      //  System.out.println("the matching contacts of this user are :" + responseContacts);

                    } catch(Exception e) {
                        e.printStackTrace();
                    }

获得本地联系人后,您有两组联系人,因此可以轻松查看json阵列联系人中显示的号码。 然后您可以在初始化期间将responseContacts传递给SelectPhoneContactAdapter,并且在适配器的getView()方法中,您可以知道是否需要输入单词&#39; - app用户&#39;到您的项目视图。