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
Related
I want to ask one question, should I use ListView inside ListView item? Or I should redesign and remake my idea?
Example of ListView, but should be with couple sub items:
A ListView inside a ListView item is never a good idea. If you want sublists inside your list you can, for example, use a ExpandableListView. However, with the migration from ListViews to RecyclerViews I would recommend you go and implement this instead.
A ListView will never work correctly embedded as an item in another ListView. In fact, scrolling things inside other scrolling things is almost never what you really want to do, as it would be confusing to the user how to actually navigate your screen.
I have an app that shows news from a RSS feed. There is ListView-1 which loads the news and when you click on an item(news), it shows only the selected news in a seperated xml layout. On the ActionBar for the single news item layout, user can click on Add to Favourites.
I have another activity and layout file and a listview just for Favourites. How can I copy an item from one listview to another?
So, the user can add news from main ListView-1 to another list called ListView-2.
I cannot use intents for sending one list item.
I have been thinking of using SharedPreferences to share an ArrayList, but something else needs to be the solution.
You don't need intents, just separate view and data, keep news in the singleton class, for example NewsManager, and keep one list for all news, one for favourite news, and use favourite list in the ListView2 adapter
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'.
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);
I'm thoroughly confused about the life cycle of list view. More specifically, what does the list adapter do exactly? Does it just provide data to the given view? And when/where does the getView() function gets called? And what purpose does this getView() function provide? From just looking at the code, it looks like getView() is "assigning" data to the view to be displayed. I'd like to be able to use list views without having to memorize, do this and then this in order for it to work. I'd much rather understand it so I can use it properly. Someone please help me understand all of this.
Also, if someone can explain to me.. what's the difference between BaseAdapter and ArrayAdapter? and any other kind of adapters that comes with Android.
What I have understood is your adapter constructor instantiated by activity and then on activity launch the getView() method is called. the {#param position, view, viewGroup}
position: it refers to the position of the view as given by adapter. Please Note it is different from the position in {OnItemClick(AdapterView adapter, View v, int position,long id)} here position is the list item position. The {position} in {getView()} changes after particular object in the list are displayed again for eg. when you scroll.
view: the view here is the view you want to be presented through getView(). It can be a particular XML layout for each row. So this states clearly that getView is called to plot every row. this view needs to be valid one or another layout (LinearLayout by default) will be selected to maintain uniqueness.
viewgroup: as you might know and as name says will be the container of your #param:view
any other point is appreciated.
getView() fills in the data into the item's view with the given index. The view which is given as a parameter may be a pre-inflated view. If it is not, you have to infalte it yourself.
An ArrayAdapter simply calls setText on the given view with the result of toString() of the object with the respective index from the array. If you override it, you can do more complex stuff, like assigning a picture or filling in more TextViews.
I recommend the following tutorial: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
Hi list adaper provides view for listview.
when user scrolls listview at that time getview is called.
getview is used to populate your view with data hence the name adapter.
The Adapter does all the "rember to do this" for you. If you change a list view's backing data structure through the adapter's methods (e.g. "add()") it will fire all the datachanged and update events you'll need for the list view to show the new state of the data.