2016-07-22 3 views
1

친구를 내 응용 프로그램에 초대하여 응용 프로그램을 사용할 수도 있습니다. 이것을 위해 나는 모든 접촉을 얻는다. 그러나 문제는 접촉이 반복되고있다. 일부 연락처는 2 번 표시되며 일부는 3 번 및 4 번 표시됩니다. 나는 "group by"쿼리를해야 중복이 나타나지 않을 것이라고 생각하지만 그 쿼리를 어디에 넣을 지 혼란 스럽다.연락처 검색시 연락처 중복 문제가 발생했습니다.

private ArrayList<String> conNames; 
private ArrayList<String> conNumbers; 
private Cursor crContacts; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.invite); 
    conNames = new ArrayList<String>(); 
    conNumbers = new ArrayList<String>(); 

    crContacts = ContactHelper.getContactCursor(getContentResolver(), ""); 
    crContacts.moveToFirst(); 

    while (!crContacts.isAfterLast()) { 
     conNames.add(crContacts.getString(1)); 
     conNumbers.add(crContacts.getString(2)); 
     crContacts.moveToNext(); 
    } 

    setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, 
      R.id.tvNameMain, conNames)); 

} 

private class MyAdapter extends ArrayAdapter<String> { 

    public MyAdapter(Context context, int resource, int textViewResourceId, 
      ArrayList<String> conNames) { 
     super(context, resource, textViewResourceId, conNames); 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     View row = setList(position, parent); 
     return row; 
    } 

    private View setList(int position, ViewGroup parent) { 
     LayoutInflater inf = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View row = inf.inflate(R.layout.liststyle, parent, false); 

     TextView tvName = (TextView) row.findViewById(R.id.tvNameMain); 
     final TextView tvNumber = (TextView) row.findViewById(R.id.tvNumberMain); 
    tvName.setText(conNames.get(position)); 
     tvNumber.setText(conNumbers.get(position)); 
     return row; 
    } 
} 

당신이 컬렉션에서 중복을 원하지 않는 경우, 당신은 HashMap을 사용하는 것이 좋습니다 중복 된 연락처

답변

2

@Arslan 알리

을 피하기 나를 수행 할 작업 안내 : 다음은 내 코드입니다. 중복 키는 허용하지 않지만 중복 값을 허용합니다. 그래서 당신은 contacts namenumber을 가져 왔습니다. 번호는 항상 고유하므로 숫자와 키와 이름을 값으로 사용하십시오. 귀하의 경우

Map<String, String> hm = new HashMap<String, String>(); 

while (!crContacts.isAfterLast()) { 
     hm.put(crContacts.getString(2),crContacts.getString(1)); 
     crContacts.moveToNext(); 
} 

너무

그래서이 방법 당신은 반복적 인 접촉을하지 않습니다. 이제 HashMap 다음은 conNames

for (String key : hm.keySet()) { 
    conNames.add(key); 
    conNumbers.add(hm.get(key)); 
} 

conNumbers 이름 값에 키 값을 밀어 반복 그리고 당신은 고유의 ArrayList를 얻었다.

+0

좋아, 너를 잡았어, 이걸 사용해 보자. –

+0

매력처럼 작동한다. 고마워요 :) –

+0

@ArslanAli 환영합니다 동생 – eLemEnt