6

I have created a List of Contacts that's working but when I click on any contact I only get contact Number of 1st Item on the android screen from the ListView.

I want to get Phone Number of clicked contact of that position. I have searched on web everywhere but didn't get any solution, i am trying to solve this issue for last 13 days but still i am unable to solve this, If anyone can resolve this issue please answer. Thanks for your help brother/sister!


li.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

TextView txtNummber = li.findViewById(android.R.id.text2)
}
});
Gopal Meena
  • 407
  • 8
  • 20
  • if the app is crashing, please post your stack trace. see: https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – marmor Jun 23 '20 at 14:03
  • Can you provide the xml code too, so I can recreate it in Android Studio and try to solve the problem? – Cardstdani Jul 05 '20 at 12:33
  • the cursor is your data structure, and position is the element location in your data structure, so use position from inside your setOnItemClickListener. – Rafiq Jul 05 '20 at 12:54

2 Answers2

5

In your onItemClick(), you're using li.findViewById().

But, onItemClick() returns View as an argument of the clicked item, which is also mentioned in the official documentation.

Hence, try updating it as

TextView txtNummber = view.findViewById(android.R.id.text2).

Check if it works.

Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50
  • 1
    It Worked. Thanks Bro! can you tell me how to filter contacts and get the searched contact like in our contact's app? I will add 50 bounty more to this question. – Gopal Meena Jul 05 '20 at 12:52
  • @GopalMeena Further, to filter the list with `SimpleCursorAdapter`, you can check [this code](https://riptutorial.com/android/example/17285/filtering-with-cursoradapter) or [this](https://stackoverflow.com/a/5068032/8244632). – Lalit Fauzdar Jul 05 '20 at 15:04
1

You can use this Contact Lib for any kind of filters. I would recommend you to use Recyclerview instead of ListView. To implement search in RecyclerView check this out.

Recylerview performance is better than Listview. Let me know If you have any issue in the implementation.

Amit Kumar
  • 438
  • 1
  • 4
  • 15