'2017/07/09'에 해당되는 글 1건

728x90

안드로이드 연락처(Contacts) 에 있는 데이터를 메모리로 읽어오기 위한 방법은 ArrayList 를 여러개 선언하는 방법도 있지만, Contact_Item 클래스를 만들고 HashTable 를 사용해서 메모리에 저장하는 방법이 좋다.


Contacts_Item 클래스를 정의하는 방법은 http://link2me.tistory.com/1251 게시글을 참조하면 된다.

여기서는 결과를 적는다.

단순하게 연락처에 있는 데이터를 가져오는 클래스가 아니라 서버에 있는 데이터를 연락처에 내려받아 저장한 데이터를 가져오기 위한 것이라 형태가 다른 contactKey 를 추가했다.


 public class Contacts_Item {   
    String contactId; // 연락처(Contacts) Contact_ID 로 데이터 수정/삭제 키 값
    String contactName; // 연락처 표시이름
    String contactmobileNO; // 연락처 휴대폰번호
    String contactofficeNO; // 연락처 사무실번호
    String contactKey; // 서버와의 데이터 동기화를 위한 키

    public Contacts_Item() {
    }

    public Contacts_Item(String contactId, String contactName, String contactmobileNO, String contactofficeNO, String contactKey) {
        this.contactId = contactId;
        this.contactName = contactName;
        this.contactmobileNO = contactmobileNO;
        this.contactofficeNO = contactofficeNO;
        this.contactKey = contactKey;
    }

    public String getContactId() {
        return contactId;
    }

    public void setContactId(String contactId) {
        this.contactId = contactId;
    }

    public String getContactName() {
        return contactName;
    }

    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    public String getContactmobileNO() {
        return contactmobileNO;
    }

    public void setContactmobileNO(String contactmobileNO) {
        this.contactmobileNO = contactmobileNO;
    }

    public String getContactofficeNO() {
        return contactofficeNO;
    }

    public void setContactofficeNO(String contactofficeNO) {
        this.contactofficeNO = contactofficeNO;
    }

    public String getContactKey() {
        return contactKey;
    }

    public void setContactKey(String contactKey) {
        this.contactKey = contactKey;
    }
}


연락처 Hashtable 를 선언, 등록, 사용하는 방법이다.

HashMap 에 대한 기본 개념은 http://link2me.tistory.com/1210 참조하면 된다.


선언

HashMap<String, Contacts_Item> contactMap = new HashMap<String, Contacts_Item>();


등록

temp_ID 와 temp_keyIDX 는 개념 설명이므로 실 코드는 기록하지 않는다.

Contacts_Item item = new Contacts_Item(); 객체를 생성하고 데이터를 저장한 다음, 선언한 HashMap 에 저정한다. contactMap.put(temp_key, item);


public void Contacts2ArrayList() {
    contactMap.clear(); // 메모리 초기화
    Cursor cursor = ContactHelper.LoadContactsCursor(context.getContentResolver(), search_name);
    cursor.moveToFirst();
    System.out.println("연락처 개수 = " + cursor.getCount());
    while (!cursor.isAfterLast()) {
        String ContactId = cursor.getString(0);
        String temp_key = "";
        if (temp_ID.indexOf(ContactId) > -1) {
            temp_key = temp_keyIDX.get(temp_ID.indexOf(ContactId));
        }
        if (!temp_key.equals("")) { // if 문은 상황에 따라 사용여부 결정하면 된다.
            Contacts_Item item = new Contacts_Item();
            item.setContactId(ContactId);
            item.setContactName(cursor.getString(1));
            item.setContactmobileNO(cursor.getString(2));
            item.setContactofficeNO(cursor.getString(3));
            item.setContactKey(temp_key);
            contactMap.put(temp_key, item);
        }
        cursor.moveToNext();
    }
    cursor.close();
}


검색

if (contactMap.containsKey(idx)) {
    Contacts_Item item = contactMap.get(idx);
    String contactName = item.getContactName();
    String contactId = item.getContactId();
    String contactmNO = item.getContactmobileNO();
    String contactoNO = item.getContactofficeNO();

   

    // 실제 처리할 메소드 추가하면 된다.

}


블로그 이미지

Link2Me

,