** Current Situation: **
One Activity, with one ListView, that changes its contents based on resetting the adapter between three different adapters. There is just one ListView with id : list. Thats it. When I reset adapter it adjusts the content of the list view.
What I want to do:
Use ViewPager to page between the different list instantiations.
Problem: View pager seems to be setup to take separate layouts, but I have written all my logic against a single list. It would be a big rewrite to use three different ListViews pointing to differnt list.
Question: How do I use the ViewPager to switch between views of the same list that are generated by different adapters being applied to the list?
Just use the same fragment for every page. Set a single variable in the intent that tells the fragment which adapter should be used. Technically it's not the same listview, although it is the same code (which is what usually matters I think).
I don't think it's possible for it to technically be the same listview considering the swiping animation.
Related
I have looked everywhere for examples on how I can have individual fragments set up as in a list view. It seems either this is highly not recommended or not possible. Does anyone have examples that I can look at? Essentially the app that I am trying to build is a an e-commerce app with a list of items to purchase. I know it may be overkill to have every item to be listed as a fragment but for this project, I need to do this.
This is highly not recommended. If your using a ListView then your xml layout file for each list item will be the same. Ideally if you use a ListView your elements will be mostly the same. You could have one Fragment for when an item is selected from your ListView, however. This one Fragment will be able to function properly for all elements of the ListView. That way you only have on Fragment for all of the elements in your ListView. Having a Fragment for each item in a ListView would be very inefficient.
How can I use same adapter class for multiple screen(different task) under same project.
Tasks are:-
Suppose:-
one screen has Grid view image with text
second Screen List View only text
same adapter for drawer Layout
is possible to use same custom adapter for every classes???
I would recommend not to create the same adapter in that case. Maintain different Adapters for that, Because in the future You would need different animation for both or some specific changes.for list/grid in that case you will end up writing lot of if-else in getView(), that will make your listview/gridview slower
Best way is, you need to create separate CustomAdapter for both . If you have same type of layout means you can use same Adapter. otherwise create new one
When it comes to a screen that contains 2 different lists depending on the state of say a checkbox at the top, is it better to use 2 listviews that you hide/show when the checkbox is selected or should I have 2 different adapters and attach/detach each from a single listview?
Are there any benefits/downsides to either of these solutions?
You should go with 2 adapters and 1 ListView. the reason is simple - either way you are going to have 2 Adapters (as per your question). Having one ListView would make the code simpler and there will only be a single instance in your xml file. Depending upon the state of the checkbox, you just need to change the adapter the listview points to and notify of this change.
If you had two list views, you would have to hide one. Just because the ListView is hidden doesn't mean that Android does not have to bother about it. You just have an object (size will depend) consuming resources sitting in the background.
Both of the proposals are wrong.
If we assume that you have same Object type to be listed in a ListView (Let's say a user object with name and id fields), then you need to have one ListView and one Adapter. When user wants to switch the data, what you need to do is to send the new data set to your adapter and then refresh it by calling notifyDataSetChanged(); method of adapter.
I am developing an activity with a ListView in which I need to change the current row by another layout by clicking on the row, and I'm not finding any way to do as much as I look (I take hours searching for possible solutions and I have not seen any reference to this problem). I do not know if this can be done in Android, but if anyone has an idea of how to do this would be appreciated.
Thanks in advance.
PS: The ListView control is normal and just want to replace a layout with a different layout. I'm using the API 15.
Use a ViewSwitcher
http://developer.android.com/reference/android/widget/ViewSwitcher.html
A ViewSwitcher is -
ViewAnimator that switches between two views, and has a factory from
which these views are created. You can either use the factory to
create the views, or add them yourself. A ViewSwitcher can only have
two child views, of which only one is shown at a time.
I suggest merging the two layouts in a single one and hide the second one. In your adapter data you should have a flag or something to indicate which layout to display. When you click a row, toggle that flag for the selected item and notifyDataSetChanged() on the adapter. This will make sure the changed layout remains even if you scroll up and down and the row goes off screen.
A more optimized solution is to have different item types in the adapter.
I'm not sure how to correctly build my application. I have an activity that has an array of items. The activity has no GUI. Further on, I have multiple fragments. Each fragment should read (filter) items from my Activity and display the items using the same views. So each item is displayed using the same view. Should I now implement an ArrayAdapter in my activity or should I create multiple ArrayAdapters in my fragments? Unfortunately I'm not used to work with adapters.
Thanks for your advice!
You can share Adapters between AdapterViews if they have the same backing data. But as a general rule that's not the case.
In short, unless all AdapterViews (e.g. ListView, Spinner) show the same items, you should use separate Adapters.