2016-07-22 22 views
1

友だちを自分のアプリケーションに招待して、アプリケーションを使用できるようにしたいだけです。このため私はすべての連絡先を取得しますが、問題は連絡先が繰り返されていることです。いくつかの連絡先は2回、3回と4回があります。私は、私はクエリを "グループで"重複が表示されないようにする必要があると思うが、私はそのクエリをどこに置くのか混乱しています。連絡先の取得時に連絡先の重複が発生する

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

関連する問題