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.
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.
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.
this might be a dummy question, but I am struggling trying to come up with a solution.
In my app I commonly use a ListView with a Header, and two TextViews for row. But right now for each ListView that shows different data I am creating a ListFragment. My idea is to keep only one class extending ListFragment and use that to show all ListViews in the app with the same structure (Header, and two TextView for row).
For example:
In this image I am using two separate Fragments. What Would be the best way to re-use the ListFragment
I want to know that what will be the best way to implement spinner in a listview. My scenario is I am getting products names, prices and their number from server side. For this I am going to make one main Activity (with one xml) that will contain the listview and a separate adapter class to make the adapter for the listview and that adapter layout. Now I want to ask is this approach will be good as I will be having some operations on spinner selection (getting each spinner (or product) dropdown number, spinner (or product) name and position) and also I will have to handle spinner positions as getview() will render each time? Or you people suggest me some good ideas instead of this?
Having many spinners in a ListView is possible but I would imagine in most cases it'd be inconvenient for the user -- too much clutter and moving elements on top of each other. I suggest you create a separate Fragment for item details, and only display static items in your ListView that will take the user to item details fragment (with spinners and anything else you might need). Take a look at this design pattern: Multi-pane layouts
** 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.