user2660030
user2660030

Reputation:

how to find contact id on item click of listview

I am beginner in android technology.I have a listview in which my contact names are shown, I want when i click on listview item, contact id of particular contact is shown in toast. I tried a lot but not able to do that.also doesn't found suitable example on google

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listcontact = (ListView)findViewById(R.id.listView1);
    getnumber(this.getContentResolver());
    listcontact.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
        /*  ContentResolver cr2 = null;
            Cursor phone2 = cr2.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            String uid=phone2.getString(phone2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));*/
            String getselected=(String)(listcontact.getItemAtPosition(arg2));
            long getsele=(long)(listcontact.getItemIdAtPosition(arg2));
            String s=String.valueOf(getsele);
            Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
            //phoneCall(getselected);
            //Intent callIntent = new Intent(Intent.ACTION_CALL);
            //callIntent.setData(Uri.parse("tel:0377778888"));
            //startActivity(callIntent);
        }
    });

    edittext=(EditText)findViewById(R.id.editText1);
    edittext.requestFocus();
    edittext.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            MainActivity.this.adapter.getFilter().filter(s);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public void getnumber(ContentResolver cr) {
    Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    while (phone.moveToNext()) {
        phonenumber=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        name=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        System.out.println("...........name");
        String list=name+
                " "+phonenumber;
        aa.add(list);
    }
    phone.close();
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,aa);
    listcontact.setAdapter(adapter);
}

Thanks in advance.

Upvotes: 1

Views: 1422

Answers (7)

Hradesh Kumar
Hradesh Kumar

Reputation: 1805

Make A Class

class Info {
  public String name;
  public String phone;
  public String id;
  @Override
  public String toString() {
    return name + " " + phone;
  } 
} 

Update your method

 public void getnumber(ContentResolver cr) {
        Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        while (phone.moveToNext()) {
              Info info = new Info();

            info.phone = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            info.name = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        info.id =phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID‌​));
            System.out.println("...........name");         
            aa.add(info);
        }
        phone.close();
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,aa);
        listcontact.setAdapter(adapter);
    }

listcontact.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
         int  Id= ((Info)aa.get(arg2)).id; 
        }});

Upvotes: 1

user8938
user8938

Reputation: 559

You can store contact id at the time of retriving contact and set tag on listview item. After that you can get id onItem click event of listview by v.gettag()

Upvotes: 0

Pratik Dasa
Pratik Dasa

Reputation: 7439

First of all add your all the data to HashMap or in ArrayList. Then when you click on row, at that time use that ArrayList or HashMap to fetch the data whatever you want.

i.e.

 ArrayList<String> idArray=new ArrayList<String>();

idArray.add(YOUR DATA);

Then, When you click on row,

String id= idArray.get(arg2);

i.e.

 ArrayList<HashMap<String, String>> mainData=new ArrayList<HashMap<String, String>>(); 
HashMap<String, String> data=new HashMap<String, String>();

data.put("id", YOUR ID DATA);
data.put("name", YOUR NAME DATA);

At the end of insertion in HashMap, Just add that hashmap into mainData ArrayList.

mainData.add(data);

Then, use this arraylist to fetch your records,

String id= mainData.get(arg2).get("id");
String name= mainData.get(arg2).get("name");

Upvotes: 0

Waseem Arain
Waseem Arain

Reputation: 1177

Try That

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
       long getsele=(long)(listcontact.get(arg3));
}

});

Upvotes: 1

Nitesh Tiwari
Nitesh Tiwari

Reputation: 4762

Try this out:

    list_View.setOnItemClickListener( new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position,
                        long arg3) {

    TextView txt_content        = (TextView) view.findViewById(R.id.txt_content);
    String uid = txt_content.getText().toString();

// work if you have uid into this textView
});

Hope this could help

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157457

you have an ArrayAdapter of String. That means the content of every row in the ListView is a String. In your case is:

 name 12121212121

you retrieve (almost) correctly the content of the row, this way:

String getselected=(String)(listcontact.getItemAtPosition(arg2));

getselected will contain something like name 12121212121.

Imo, since you are already working with cursor, You should use one of the implementations of CursorAdapter,

The simplest thing that you can do is to split the string for empty space, but of course, if the String is name surname 12121212121 it will not work that good. Another thing you can do is to wrap the information inside a class:

class Info {
  public String name;
  public String phone;

  @Override
  public String toString() {
    return name + " " + phone;
  } 
}

your adapter will be of Info and

String getselected=(String)(listcontact.getItemAtPosition(arg2));

will change like

Info getselected=(Info)(listcontact.getItemAtPosition(arg2));

name will be getselected.name and phone will be getselected.phone.

Upvotes: 0

Anuj
Anuj

Reputation: 476

 @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
           int viewId= arg1.getId()// this will return you the id of that particular view
    }
});

Upvotes: 0

Related Questions