android match contact from contactlist

2019-09-02 13:46发布

问题:

I have try this code..it is working fine..

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;
    }

}  
  }

Now in ArrayList name1 I am having list of all name of my device.. and in name2 i am having Arraylist of some selcected person..Now i want to match both of this array list and display name in list that is present.... I am working on this since last week..stuck here .. Please help me... Thanks ...:)

回答1:

dont panic dude just match both list and take one temp arraylist and when any name match then add in that temp arraylist and then show that list view according to that temp arraylist :) here i give u code and hint i think it will help u to achieve whatever u want...:)

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);

                        }
                    }
}


回答2:

I solve my issue.. by refering this link..https://thenewcircle.com/s/post/1375/android_content_provider_tutorialHope this will help to someone