Android setOnItemClickListener vs setOnClickListener - android

As I know, there are two methods to handle click on different list items:
Use setTag() to set types for items of list in Adapter, then setOnItemClickListener() for the list and getTag() of the view to differentiate the type, like this:
listview.setOnItemClickListener(new OnItemClcikListener(){});
Inside the adapter, setOnClickListener() individually for each item during getView(), like this:
item.setOnClickListener(new OnClickListener() {});
What is the difference and which one is preferred?

OnItemClickListener is very easy to manage compare to OnClickListener.
If you still would love to manage OnClickListener I will tell why OnItemClickListener is much better than OnClickListener.
Once you start scrolling ListView items will start getting reused and you ended up creating lot of OnClickListener. Don't worry, this is not a memory leak as GC will come into picture and collect those but you should not feel safe also because GC pauses your activity even if it is fraction of second that's considerable.
So would I go with OnItemClickListener unless you planned something different for individual list item.
If you need to create specific portions of each item to be clickable or want more than one action to be performed for a given item, it would be best if possible to collect those actions into a single OnClickListener that is created once and then attached to each item in getView(). You can differentiate which item was clicked by attaching metadata about the click action and maybe list position to the views themselves with setTag().

I'm not sure i really understood what do you mean but i will try to give you a reply.
Use setTag() to set types for items of list in Adapter, then setOnItemClickListener() for the list and getTag() of the view to differentiate the type, like this:
listview.setOnItemClickListener(new OnItemClcikListener(){});
You can use setTag() for set an Object like a tag, it means you can use it to put some informations into your cell view (ex: text, id and so on).
To "differentiate the type" of your view i would suggest you to use `getViewTypeCount().
inside the adapter, setOnClickListener() individually for each item during getView(), like this:
item.setOnClickListener(new OnClickListener() {});
This actually depends more on what you want to do with your list, if the behaviour of the click is strictly related to the informations which belong to your adapter or for example if you have a button inside your cell view, then set a listener for the cell view inside the adapter could be a good solution.
But except for the last case i would say the first is the best solution, since you can put everything you want into your tag, and it gives you the chance to manage your list's click from your main Activity or Fragment.

Related

Android - Get item from listview with custom adapter through checkbox

I have a listview with a custom adapter (extends BaseAdapter).
It recieves a list of objects I have to populate a ListView.
One of my object's atribute is a boolean called "checked".
on my method getView, this atribute is responsible for checking or not checking a CheckBox on my View.
Everything is working just fine and when my Activity loads, the ListView itens apear as they are on my list of objects (which was received from my database), some checked and some not checked.
But when I check one of my ListView's checkbox, I need to update my object and therefore it's value on my database. The problem is:
"How do I know which item (object) I have to update just by checking my CheckBox?"
"Don't they have the same name?"
I have a listView.setOnItemClickListener(...) where I can get my object by it's position, but it works when I click on the "row" of my list view itself, not on my checkbox... I thought about using it to check/uncheck my CheckBox... But how would I do that? Can I use the position to get a specific CheckBox from my listView?
In the end, I also thought that the best method would use the "listView.setOnItemClickListener(...)" to check my CheckBox, once it would be easier for my user to check one Item by it's row than by a tiny CheckBox, so can someone help me with the best way to solve my problem?
I'm sorry I didn't post my code, but right now I can't access it.
Take a look at this tutorial
You need to add an method in the onClick of the checkbox that will be implemented by the activity. One way to do that is to add an abstract method to the adpater and make the activity implement it. lets call onCheckBoxClicked(int position)

Which: ListView.setOnItemClickListener or TextView.setOnClickListener?

I have a ListView with a custom adapter that only has a TextView, and I want to set a click listener for it.
Which one I should choose: ListView.setOnItemClickListener outside of the custom adapter, or TextView.setOnClickListener inside the custom adapter's getView()? And why?
Thanks!
L.
You should definitely use ListView.setOnItemClickListener
Because when you press the list item it gives feedback that you pressed it (like glow background or something)
You are controlling your data from outside of your list, and therefore you have better vision on the objects you are controlling
More object oriented
In my opinion it's easier
If you have only one view in a list row then why bother setting the click listener on the TextView instead of the row?
adding onClickListener to views in the getView() method is using when you have 2 or more views that should have their own onClickListener,so for your is better onItemClickListener

Handling onItemClick event with ListView and complex layout

This is a request for a best practice for handling the views in the onItemClick events in a ListView.
Each view in my ListView contains an ImageView, some TextViews, and a CheckBox wrapped in a RelativeLayout. On the item view being selected anywhere, I'd like to have the checkbox ticked/unticked as required. I would also like to have the option to change the view style i.e. so the item is highlighted by colouring the stroke.
My current issue is similar to everyone else who does this. Due to ListView recycling, if I grab the View passed into onItemClick and set the backgroundresource or checkbox checked value, it also changes them for Views that aren't currently rendered. I'd like to know the best way to achieve this goal.
I know there is a pattern to handle the CheckBox events in the ArrayAdapter directly by implementing an onClickListener, but I believe this will only respond to direct selection of the CheckBox; it will ignore any clicks on the rest of the view. Plus, if the CheckBox onClick event is fired, it will consume the click event so onItemClick won't fire, which I need to set my data.
Also, using CheckedTextView will not work since it is inside the RelativeLayout, which won't pass the checked event to the ListView. There was a post where it was recommended to create your own CheckedViewLayout that implements Checkable to handle the View checked events. If I go ahead with this, will I be able to reference my new layout class in xml, or would I then have to design each of my views again but in Java code?
If anyone else has a better solution, then please let me know.
Hey I think this link may be useful to you,there is a details for you
Android ListView Checkbox Example - OnItemClickListener() and OnClickListener()
In the end, I created my own CheckableRelativeLayout that implemented Checkable and used it as the main container for each ListView item. I used a Boolean value within CheckableRelativeLayout to track whether the view was clicked or not.
If required, I can post the final class.

Preselect items in listView Android

All i need is an idea or path to continue searching, i have a list of items that i add to a listview adapter. And i am trying to make some items in the list to have a specific background color (as an example) when the activity loads. Something like having those rows "preselected". The method public ListView getListView() does not help me much.
Thank you either way.
Need to set Multiple Choice Mode via setChoiceMode(), and then you can call setItemChecked() for each of the items you want checked. Then you probably have to make a custom adapter that overrides getView(), cast the parent to ListView and ask if the View (a list item) you are getting at the position given is in fact checked by calling something like getCheckedItems() on the parent. Then you can do what ever you want with that view, like set its background color etc.

ListView and propagating click events from children views in the items

I have a ListView with custom items - 3 ImageViews and a TextView. I have a call to setItemsCanFocus(true), so I can make the ImageViews clickable. I'm currently using SimpleAdapter to populate the View.
I'd like to trigger the AdapterView's onItemClick event when one of those subviews is clicked. The onItemClickListener receives a view as the second argument and that can be used to identify which subview was clicked. Frankly, I was expecting this to be the default behaviour but it isn't, unfortunately.
Is there any way to implement this behaviour without bluntly breaking encapsulation (i.e. creating an Adapter that holds a reference to its View)?
What is the accepted way of dealing with events from views in list items? How do you keep the Adapter from knowing too much about the ListView?
Unfortunately you have to choose between using onItemClick() or onClick() on individual children. One way to do it however is to make the top-level view of each item clickable.
Setting android:addStatesFromChildren="true" on the listview in your xml will send clicks on the child elements to the onItemClick method in the onItemClickListener connected to your listview.

Categories

Resources