Arraylist hashmap仅添加最后一个元素

时间:2016-05-24 10:20:14

标签: android arraylist hashmap

我正在使用Arraylist<Hashmap<String,String>>在我的Arraylist联系人中,它只添加了最后一个元素。

我的代码:

public void readContacts() {
        StringBuffer sb = new StringBuffer();
        sb.append("......Contact Details.....");
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        String phone = null;
        String emailContact = null;
        String emailType = null;
        String name = null;
        String image_uri = "";
        Bitmap bitmap = null;
        ArrayList<HashMap<String, String>> Contacts;
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                Contacts = new ArrayList<HashMap<String, String>>();
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                image_uri = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    System.out.println("name : " + name + ", ID : " + id);
                    sb.append("\n Contact Name:" + name);
                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
                    while (pCur.moveToNext()) {
                        phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        sb.append("\n Phone number:" + phone);
                        System.out.println("phone" + mPhoneno.toString());
                    }
                    pCur.close();
                    Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
                    while (emailCur.moveToNext()) {
                        emailContact = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                        emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
                        sb.append("\nEmail:" + emailContact);
                        System.out.println("Email " + emailContact + " Email Type : " + emailType);
                    }
                    emailCur.close();
                }
                if (image_uri != null) {
                    System.out.println(Uri.parse(image_uri));
                    try {
                        bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(image_uri));
                        sb.append("\n Image in Bitmap:" + bitmap);
                        System.out.println(bitmap);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block e.printStackTrace();
                    }
                }
                sb.append("\n........................................");
                HashMap<String, String> map = new HashMap<String, String>();//put in it
                map.put("FirstName", name);
                map.put("LastName", "");
                map.put("MobileNo", phone);
                map.put("Email",emailContact);
                Contacts.add(map);
                System.out.println("Contacts List :" + Contacts);
            }

        }

    } 

我做错了什么?请指导我。在此先感谢。

2 个答案:

答案 0 :(得分:1)

你需要在while命令之前移动这一行

 Contacts = new ArrayList<HashMap<String, String>>();

每次分配一个新的arrayList

答案 1 :(得分:0)

    public void readContacts() {
            StringBuffer sb = new StringBuffer();
            sb.append("......Contact Details.....");
            ContentResolver cr = getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
            String phone = null;
            String emailContact = null;
            String emailType = null;
            String name = null;
            String image_uri = "";
            Bitmap bitmap = null;
            ArrayList<HashMap<String, String>> Contacts;
            if (cur.getCount() > 0) {
                   ///get Contacts initialization out of while 
                Contacts = new ArrayList<HashMap<String, String>>();
                while (cur.moveToNext()) {

                    String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                    name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    image_uri = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
                    if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                        System.out.println("name : " + name + ", ID : " + id);
                        sb.append("\n Contact Name:" + name);
                        Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
                        while (pCur.moveToNext()) {
                            phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            sb.append("\n Phone number:" + phone);
                            System.out.println("phone" + mPhoneno.toString());
                        }
                        pCur.close();
                        Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
                        while (emailCur.moveToNext()) {
                            emailContact = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                            emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
                            sb.append("\nEmail:" + emailContact);
                            System.out.println("Email " + emailContact + " Email Type : " + emailType);
                        }
                        emailCur.close();
                    }
                    if (image_uri != null) {
                        System.out.println(Uri.parse(image_uri));
                        try {
                            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(image_uri));
                            sb.append("\n Image in Bitmap:" + bitmap);
                            System.out.println(bitmap);
                        } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block e.printStackTrace();
                        }
                    }
                    sb.append("\n........................................");
                    HashMap<String, String> map = new HashMap<String, String>();//put in it
                    map.put("FirstName", name);
                    map.put("LastName", "");
                    map.put("MobileNo", phone);
                    map.put("Email",emailContact);
                    Contacts.add(map);
                    System.out.println("Contacts List :" + Contacts);
                }

            }

        }