联系人列表中的android匹配联系人

时间:2013-11-26 10:55:58

标签: android android-contacts

我试过这段代码......工作正常......

public class Test extends Activity implements OnItemClickListener{

List<String> name1 = new ArrayList<String>();
List<String> name2 = new ArrayList<String>();
List<String> phno1 = new ArrayList<String>();
List<String> matchedList;
MyAdapter ma ;
Button select;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    name2.add("Abc");
    name2.add("Xyz");
    name2.add("Pqr");
    name2.add("aaa");

    getAllContacts(this.getContentResolver());

    ListView lv= (ListView) findViewById(R.id.lv);
    ma = new MyAdapter();
    lv.setAdapter(ma);
    lv.setOnItemClickListener(this); 
    lv.setItemsCanFocus(false);
    lv.setTextFilterEnabled(true);



}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub

}

public  void getAllContacts(ContentResolver cr) {
    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    while (phones.moveToNext())
    {
        String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY));
        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        System.out.println(".................."+phoneNumber); 
        name1.add(name);
        phno1.add(phoneNumber);



    }

    phones.close();
}
class MyAdapter extends BaseAdapter 
{ 

    LayoutInflater mInflater;
    TextView tv1,tv;

    MyAdapter()
    {
        // mCheckStates = new SparseBooleanArray(name1.size());
        mInflater = (LayoutInflater)Test.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return name1.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub

        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi=convertView;
        if(convertView==null)
            vi = mInflater.inflate(R.layout.row_list, null); 
        TextView tv= (TextView) vi.findViewById(R.id.txt_name);
        tv1= (TextView) vi.findViewById(R.id.txt_phoneNo);

        tv.setText("Name :"+ name1.get(position));
        // tv1.setText("Phone No :"+ phno1.get(position));


        return vi;
    }

}  
  }

现在在ArrayList name1中我有我的设备的所有名称的列表.. 并且在name2中我有一些selcected人的Arraylist ..现在我想匹配这个数组列表和显示列表中的名称.... 我上周以来一直在努力..在这里...请帮助我...... 谢谢...:)

2 个答案:

答案 0 :(得分:1)

别恐慌dude只是匹配两个列表并采取一个临时arraylist和任何名称匹配然后添加在那个临时arraylist然后根据那个临时arraylist显示该列表视图:)在这里我给你的代码和提示我认为它会帮助你实现你想要的...... :)

ArrayList<String> matchname = new ArrayList<String>();
                for(int k =0;k<name1;k++)
                {
                    for(int l =0;l<name2.size();l++)
                    {
                        String keyname = name1[k];

                        if((keyname.trim().equals(name2[l].trim())))
                        {
                            matchname.add(keyname);

                        }
                    }
}

答案 1 :(得分:1)

我通过引用此链接解决了我的问题.. https://thenewcircle.com/s/post/1375/android_content_provider_tutorialHope这对某人有帮助