2014-11-10 4 views
0

내 안드로이드 폰에서 연락처를 수정하여 데이터베이스에 다시 쓰려고합니다.ArrayList는 마지막으로 읽은 값만 저장합니다

  • MainActivity.java : 이 프로젝트는 세 가지 클래스까지 있습니다. 여기에서 데이터베이스를 읽고 내가 만든 연락처 개체를 채우고 목록에 넣습니다.

    public class MainActivity extends Activity { 
    private ArrayAdapter<String> adapter; 
    
    // Store contacts values in these arraylist. 
    // I t is not really needed for this implementation but I keep this just in case 
    
    public static ArrayList<String> phoneValueArr = new ArrayList<>(); 
    public static ArrayList<String> nameValueArr = new ArrayList<>(); 
    
    // Store the values of names and associated phone numbers. 
    public ContactData contactsData= new ContactData(); 
    
    // List to store the contacts 
    public ContactsDataList contactsList= new ContactsDataList(); 
    
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    
    adapter = new ArrayAdapter<> 
    (this, android.R.layout.simple_dropdown_item_1line, new ArrayList<String>()); 
    readContactsData(); 
    
    //Display the number of contacts 
    Toast.makeText(getApplicationContext(), 
         "read " + phoneValueArr.size() + " contacts. " , Toast.LENGTH_LONG).show(); 
    
    
    pressButton(); 
    
        for(ContactData c:contactsList.getContacts()){ 
         Toast.makeText(getApplicationContext(), 
           c.getContact_name() + "Hopes on God " , Toast.LENGTH_LONG).show(); 
    
        } 
    
    } 
    } 
    
    public void readContactsData() { 
    
    try { 
    
        // Reading Contacts Name And Number 
    
        String phoneNumber = ""; 
        ContentResolver cr = getBaseContext().getContentResolver(); 
    
        // Query to get contact name 
    
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, 
          null, null, null); 
    
        // If data data found in contacts 
        if (cur.getCount() > 0) { 
    
        Log.i("AutocompleteContacts", "Reading contacts........"); 
    
        int k = 0; 
        String name = ""; 
    
        //Until there are no more contacts stored 
         while (cur.moveToNext()) { 
    
         String id = cur.getString(cur 
         .getColumnIndex(ContactsContract.Contacts._ID)); 
         name = cur 
         .getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
    
          // Check contact have phone number 
          if (Integer 
            .parseInt(cur.getString(cur 
              .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
    
           // Create query to get phone number by contact id 
           Cursor pCur = cr 
           .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
           null, 
           ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
           + " = ?", new String[] { id }, 
           null); 
           int j = 0; 
    
           //Until query is stopped 
           while (pCur.moveToNext()) { 
            // Sometimes get multiple data 
            if (j == 0) { 
             // Get Phone number 
             phoneNumber = "" 
               + pCur.getString(pCur 
                 .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
    
             // Add contacts names to adapter 
             adapter.add(name); 
    
             // Add ArrayList names to adapter 
             phoneValueArr.add(phoneNumber.toString()); 
             nameValueArr.add(name.toString()); 
    
             // Set contact name and phone number 
             // i.e parse the SQLlite query to set the POJO's 
             contactsData.setPhone_number(phoneNumber.toString()); 
             contactsData.setContact_name(name.toString()); 
    
    
             // Add Contacts to list 
             contactsList.addContact(contactsData); 
    
    
             j++; 
             k++; 
            } 
    
           } // End while loop 
    
           pCur.close(); 
          } // End if 
    
         }// End while loop 
    
        } // End Cursor value check 
        cur.close(); 
    
    } catch (Exception e) { 
        Log.i("AutocompleteContacts", "Exception : " + e); 
    } 
    } 
    
  • 나는 개체가 인스턴스화되는 클래스를 만드는 연락처 데이터 클래스입니다.

    public class ContactData { 
    
    public String contact_name; 
    public String phone_number; 
    
    public ContactData(){ 
    
    } 
    
    public ContactData(String contact_name, String phone_number) { 
    
    this.contact_name=contact_name; 
    this.phone_number=phone_number; 
    } 
    
    
    
    public String getContact_name() { 
    return contact_name; 
    } 
    
    public void setContact_name(String contact_name) { 
    this.contact_name = contact_name; 
    } 
    public String getPhone_number() { 
    return phone_number; 
    } 
    public void setPhone_number(String phone_number) { 
    this.phone_number = phone_number; 
    } 
    
    public String toString() { 
        return contact_name + ", " + phone_number; 
    } 
    
    
    public boolean equals(Object obj) { 
    if (obj instanceof ContactData) { 
        ContactData contact = (ContactData) obj; 
        return (contact_name.equals(contact.getContact_name()) && phone_number 
          .equals(contact.getPhone_number())); 
    } 
    
    return false; 
    } 
    
    
    public int hashCode() { 
        return (contact_name.length() + phone_number.length()); 
    } 
    class ContactNameComparator implements Comparator<ContactData> { 
        public int compare(ContactData contact1, ContactData contact2) { 
         return contact1.getContact_name().compareToIgnoreCase(contact2.getContact_name()); 
        } 
    } 
    } 
    
  • 개체를 가져 와서 저장하는 ContactsDataList. 내 목록에있는

    public class ContactsDataList { 
    
    
    
    public List<ContactData> contactList; 
    
    public ContactsDataList() { 
    
    this(new ArrayList<ContactData>()); 
    } 
    
    public ContactsDataList(List<ContactData> contactList) { 
    this.contactList = contactList; 
    } 
    
    public void addContact(ContactData contact) { 
    if (contactList != null) { 
         contactList.add(contact); 
        } 
    } 
    
    public List<ContactData> getContacts() { 
    return contactList; 
    } 
    
    public void setContacts(List<ContactData> contacts) { 
    this.contactList = contacts; 
    } 
    
    
    } 
    

연락처 개체는 모두 동일 내가 phoneValuerArr arraylists로 잘못 어디서 볼 수 없습니다 및 nameValueArr 모두가 올바른 데이터를 가지고있다.

ContactsDataList, contactList에는 마지막으로 읽은 연락처 만 포함됩니다.

아무도 도와 줄 수 있습니까?

답변

3

while 루프의 모든 반복에서 동일한 인스턴스 인 ContactData을 재사용하고 있지만 속성 만 업데이트하면됩니다. 반복마다 별도의 인스턴스를 만들어야합니다.

while (pCur.moveToNext()) { 
    ... 
    ContactData contactsData = new ContactData(); 
    contactsData.setPhoneNumber(...); 
    ... 
} 
+0

롤. 고맙습니다. – TheEYL

관련 문제