0

i want to find the position of the view which is the part of the list-view's custom XML file. How can find the position of the views. Can i automatically perform onItemClick?

  • try implement your own customer adapter where you would be able to handle individual item click in it. also try using recyclerview it is better approach. – karan Dec 20 '18 at 05:43
  • you can simpley get position by this int position = lv.getPositionForView(v); – mitesh makwana Dec 20 '18 at 05:46
  • Possible duplicate of [OnItemClickListener using ArrayAdapter for ListView](https://stackoverflow.com/questions/18405299/onitemclicklistener-using-arrayadapter-for-listview) – Kevin Kurien Dec 20 '18 at 06:37

3 Answers3

0

I face this difficulty. In my case when i click on textview which is part of list-view's custom XML file then the position is right, but when i click on the button which is the part of list-view's custom XML file then it always gives first position.

After i have to decided to perform automatic onItemClick and store all the values of clicked position into another declared variable which is below. See the image i used kotlin language instead of java. I stored all the map values into another variable which is declared above with public access specifier.

[Now when i perform operation on Button click i use

lvShayari.performItemClick(v, lvShayari.getPositionForView(v), lvShayari.selectedItemId)

this statement in which lvShayari is listview and performItemClick() method perform the onItemClick automatically when we clicked on button. Here v is the view, lvShayari.getPositionForView(v) method gives the position of v in my case it is button.]2

0

check out below code , hope it will help you:

listView.onItemClickListener = AdapterView.OnItemClickListener { adapterView, view, position, id ->
        Toast.makeText(this, "Click on: " + position, Toast.LENGTH_SHORT).show()
    }

for more Details follow this link

Ajay Mistry
  • 951
  • 1
  • 14
  • 30
0

@Suvidha Malaviya

Make your own custom adapter for your listview with one interface in which there is one method with three parameters namely View, Position, Model class of your list which you are passing in your adapter.

After making this interface initialize that interface in your adapter and set that interface method in your view click event.

Now set this interface in your activity or fragment where you are using your custom adapter and get the value which you want...

For example, see below link in which there is explain how to use custom listener in list view adapter. Here you have to just make your own click listener in which you have to pass three parameters which I have mention above.

Try it. I hope you will resolve your issue...

Hemal Patel
  • 114
  • 1
  • 11