Clicking on a ListView - java

I have a ListView with a header and a footer. I added two items with a class of my own extending the ArrayAdapter class. In this extended class, I have overridden the getView function because I want Views which are not TextViews to appear in my ListView.
On this ListView, I have set an onClickListener.
The problem is that this onClickListener is started when I click either on the header or on the footer, but never when I click on my items.
Of course, the View returned by getView is set to be clickable.
What am I missing?

Do you want to do something different when each list element is clicked? If so, you should programmatically set an onClickListener for each of those 2 elements added. Can you post your code?

It sounds like you are making this much more complicated than necessary. You can create your own layout for each row of a ListView using an XML file. This allows you to use any combinations of Views that you wish. To control what happens when the user clicks on a row in the ListView, your activity class should extend ListActivity and override onItemClickListener(). Alternatively, you can call setOnItemClickListener() on your ListView. For more details, I suggest that you read the Android ListView Developer Guide.

The solution I found : the click listener attached to the 'ListView' is only used for the clicks on the header and the footer. For my custom rows, in my ArrayAdapter's 'getView' function, I attached one listener to the 'TextView' and another to the 'ImageButton'.

Related

Listview filter through same names Android

So I have a custom spinner header, where the user can change the title of names based on selection. What I want to do is if they click on the spinner and change the default, then it only shows the names in the listview from the title of names. Currently I have tried this by reinstantiating my fragment from my tabhost, and if conditions are met it would just remove from list everything that doesnt have what user selected. But this is inefficient and it didn't work as the listview wasn't updated and the condition wasn't even getting called on the new instantiation... Is there any filter way I can do for this? Any ideas or help would be great!
You only need to refresh the ListView not the entire fragment. There are several ways to do this, including providing a different data source to the ListView's adapter. The exact details for how to do this depend on whether you are using an ArrayAdapter or a CursorAdapter.

Android: modify item on Listview

I have a ListView.
I'm using anArrayAdapter and I'd like to:
when the user clicks any item on the list, its LinearLayout (is just a content for x information) fades out and is substituted by other LinearLayout (with y information) which fades in.
However, I do not know how to apply this on my ArrayAdapter. I've searched for a while but I'm not understanding how can I access a single item from the Adapter and make it's children fade out or fade in.
Any help would be very appreciated. Thank you very much for your time.
In general - adapters do not hold items the role of adapter is to get data model, and to produce item views when i.e. list view needs them.
The main concept of replacing item is to replace data inside the adapter and call notifyDataSetChanged(); - thats all.
I don't like ArrayAdapter as it's aimed for very simple scenarios. Usually I just create my own Adapter class extending BaseAdapter (just 4 methods to implement).
The transitions of items on ListView are described here: Adding animation to a List View in Android

Android ListView not showing newly added items and erasing old items when switching content views

I have a custom listview defined in my xml layout file. I can add items to this ListView inside onCreate method, through an array adapter.
However when I add items from another content view and then go back to the content view with the ListView all the items are gone and there's nothing listed. Even after calling .notifyDataSetChanged();
It seems like I can only add to the list when the content view containing the ListView is currently being displayed. Is this the default behavior?
Failed attempted workaround
I used another array to keep the newly added items and then try to add them when the ListView became visible again. I had to override onContentChanged() to do so but then no items were added still.
So the main question is
How can I dynamically add items to the ListView even if it's out of sight and still preserve the old items?
PS: I have to say the Android API is one of the worst I've ever come across.
If you change content view, then all the previous views are going to be destroyed. Are you using an adapter? If so, then it would be very easy to add all the items to the list again.
There shouldn't be any reason to setContentView any time other than in onCreate.
If you were looking to have multiple screens, instead of changing content view, then start a new activity.
dynamically add items:
//add at the top of the list
mListView.addHeaderView(itemView);
// add at the bottom of the list
mListView.addFooterView(itemView);

Android -- how to use onlistitemclick() function when am using a custom list view

Normally when an item is clicked in an listview, onlistitemclick() would get called.Since I am using the get view function to show three text views and an image in each row of the list. so the onlistitemclick() function is not getting called. anyone knows a work around for this??
Thanks in advance.
Sriram.V
I guess you should set setOnItemClickListener to your listview irrespective of your getview() customisation

How to get getSelectedView() to work in GridView?

I have a GridView in a layout. It is populated with Foo views by the activity using a extended BaseAdapter.
When I select an item in this grid it gets orange tinted (thus selected). That's nice. But I want to access this selection from outside the GridView and it's parent activity: from within another View higher in the layout hierarchy. I therefor call upon gridView.getSelectedItem(). However it always returns null.
How could I get this to work?
"Selection" doesn't mean the same thing in AndroidOS as it does in other UIs. In particular, there isn't any "selected item" when you're in touch mode. You probably need to use a click listener instead of relying on there being a "selected item". See this article for details.
You can use the following to get the view:
View childView = gridView.getChildAt(position - gridView.getFirstVisiblePosition());

Categories