Android button in listview get position

I don't what's wrong with my code. First of all, i am kinda new to android programming as i just recently move from web programming to android programming.


  • Android Custom ListView with Image and Text;
  • Your Answer.
  • Building your Android UI: Everything you need to know about Views.

I have a listview and it's scrolable. I have few listview items and it's pulled from database. My problem is, each listview items have a button and i've setOnclickListener to the button inside getView.

Now let's say there's 5 items and i tap on button for item number 1, the position is 0 and when i scroll to the end of the list, i can see the button for item number 5 is clicked. When i scroll till middle of the list, sometimes the button randomly clicked and i can see from my logcat, the particullar button's position is 0. How come?

Android Listview With Image And TextView Example/Tutorial

Can you explain why you are trying to manipulate the same button which you just did click? Hey, Thanks for the reply. An user able to like an item and the same user also able to unlike or dislike the item which they've liked. I solved the issue with a like flag to determine if the specific button is clicked or not and from there i am able to develop my code logic.

2. Writing the Adapter Class

This is my listview click event: lv1. Can anyone say me how do I get the positon? G 1 1 gold badge 6 6 silver badges 29 29 bronze badges.

RecyclerView + JSON Parsing - Part 6 - ON ITEM CLICK LISTENER INTERFACE - Android Studio Tutorial

Where have you declared the position globally? Pasquale Anatriello Pasquale Anatriello 2, 1 1 gold badge 12 12 silver badges 16 16 bronze badges. Hello sir, Please tell me how can i get the position of particular element without clicking on that element just basis of element name. NikhilSingh you should open a question on your own, or use the search function. Anyway, the method public View getView int position, View convertView, ViewGroup parent of Adapter class already containts a parameter indicating the item position.

Using tag is a bad idea. For instance, you can't call setTag on an ImageView if you're loading with Glide.

Scroll listview to specific position in android programmatically

Karoly Karoly 3, 3 3 gold badges 26 26 silver badges 48 48 bronze badges. OMG, you are a life saver. Tomorrow I have a deadline for my app at work, and just today I realised, I wasn't getting the right position and spent whole day. Thank you PromotionArtwork : , and what about me? This helped me a lot. I were using the TAGs for something else and wasn't able to find good solution. If you are using ViewHolder make sure you use setTag like this.

Adding Click Listeners to Views in Adapters | Scott Weber

Lazy Ninja Lazy Ninja Only Solution that worked!!!! Thank you so much. Can any body tell how can i find the position of particular element without clicking on that element, on the basis of element name. Ahamadullah Saikat. Sign up or log in Sign up using Google.