I have a hierarchical data structure. As an example, lets consider my data television shows. Each show has multiple seasons and each season has several episodes. I want to have a list that displays all shows. When a show is clicked the list will then display all seasons for that show and when a season is clicked the list will show all episodes for that season.
OK so with that out of the way, currently I have ONE Activity that implements a Loader through LoaderCallbacks. This loader returns a full hierarchy of data (AKA all shows with all seasons with all episodes). I then use a different ArrayAdapter depending on the level of data I'm on.
Is this the 'right' way to handle this kind of data? Should I instead have a loader and activity and adapter for each level of the data?
Sorry if this is confusing, thank you in advance for your insight.
There really is no right or wrong here. I would go for one activity and adapter per set of data. This will make your activities less complex and make them decoupled from each other. In your case I would have:
ShowActivity+ShowAdapter
SeasonActivity+SeasonAdapter
EpisodeActivity+EpisodeAdapter
Your data model (with Shows, Seasons and Episodes) should be made accessible to the activities and used to initialize each activity and adapter with the correct data.
Once a show has been selected I'd pass on the id of the show to the next activity through the intent bundle, pick up the show id in the SeasonActivity, fetch the relevant seasons from the data model and display them. The same goes for the transition from season to episode.
Using the approach of one activity per list you'll also get nice transitions between activities and don't have to worry about dealing with the back button to navigate up through your hierarchy.
Related
I have used Android Studio example with tabs and fragment, but I have some different idea. I need to automatically generate tabs and fragments, which will contain listview and will be populated using JSON.
I am successfully fetching data from JSON and placing them in one activity, but my intention is to use tabs which will serve as categories, so each time I click on different category it will show different news. My intention is that first time user clicks on tab/category, app should populate it with news, and next time he gets back it will not reload, it will show what is loaded before unless he wants to refresh it.
Refreshing category should be done by pulling down.
So, should I use one fragment as template, as all news will be presented the same way and populate it with data, or will I need to create fragment for each category?
If I will need to create different fragments for each, then in order to add new category I would need to update app version.
No need to create different fragment for each category. Just change the adapter content as you load data from server. But data for each category must be same ,means json object must have same keys in all categories. Otherwise use different fragments for each category of news.
Why dont you use pull to refresh on each tab fragment. And apply the condition, if pull to refresh then load new contents or refresh the data... or else display the old data as it is. Hope it helps you!
This question is more my way of gauging a better understanding of the "Proper" way to handle this content flow instead of just "whatever works" or the "quickest" solution to code. Im looking for the best on performance and user experience.
The situation:
So my main activity handles my NavigationDrawer and is the basis of the app. The initial view that is loaded (navitem 0) is a Fragment which contains a RecyclerView(custom adapter, list item model, view holder). This list displays data pulled from an XML file and is returned as an Arraylist of Topic objects (each containing 3 strings and an array of Issue objects).
The array of Topic objects are used to populate the listitem w/ a title, desecription and image_name strings. (Not using the Issue array yet).
Here is where you come in ...
I know need to handle the click event on the Topic and display a new list (w/ different adapter) of the specific Issue object array for that Topic.
I'd like to know if its better to replace the current fragment w/ a new fragment for handling the Issue data. Or would it be better to launch a new activity to display the Issue list data.
Keep in mind, i want to ensure that navigation up will return the user to the previous view. ie. when clicking a Topic you should get the Issues for that topic. When going back, the TopicFragment should be displayed w/ its initial list.
If this is confusing you?
The core part of this question is needing to know the proper navigational way of displaying a List that when clicked needs to display another List specific to the parent object. Is fragment to fragment handled by callbacks in the MainActivity the best way, or is Intent'ing to another activity to handle the 2nd list better?
Whether you use a Fragment or an Activity to display the second list, it doesn't matter from a performance standpoint. If I were you, I'd use an Activity: it is always better to use a Fragment only in situations that require the explicit use of Fragments (such as a FragmentTabHost or a ViewPager).
But I do have another suggestion for you. Instead of going to another list, why not display your Issue objects as the child items of an ExpandableListView, and the Topic objects as the parent items ? Then when the user clicks on an Issue child item, go to the detail page containing details of that Issue object. To me, the List->Detail pattern is a far more familiar idiom than a List->List->Detail flow. Its what ExpandableListView was made for.
Im trying to create a form in Android in which I have a form with list of items, these items are dynamic so you can add and remove them.
The question is regarding the adding. I allow the user to select from a list of things to add. Let say are products
so the user see in the form the products he/she has added but if the user wants to add more clickc a button and gets a screen with the products that can add to his form.
Now Im handling this with Fragments.
The form is in a fragment where we have all the logic of it, and the "add products" its another fragment but this is a list that have a checkbox on it so the user can select more than one at a time. so Im ding this with an ArrayAdapter and on the getView of the adapter we add the check listener to add or remove the item from the "main list"
This approach is giving me problems, passing the values back and fort from the "form fragment" to the "select products" fragment is not clear.
I wonder if is a better approach have the list on another activity and call startActivityForResult.
I think that way I would have better control over the list even the layout of the list because being a fragment and not a listFragment takes me out some possibilities.
I wanted to ask what would be the best practice or the better way to do this.
Create another activity and use startActivityForResult or try to do it with fragments.
and if by any chanse you have reference to some code sample that I can use as a how to.
for example gmail you get a big nice clean list where you can select more than one.. I want something like that behavior could that be in a fragment or has to be on a listFragment
The ListFragment you use to list products should propose an interface to send selected items. This interface would be implemented by activity or whatever to receive the list.
Then two ways to handle these item lists :
- Create an activity for results. That way, the activity implements the interface to return the list as result.
- Add the ListFragment to current activity. The fragment which want the item list implement the interface. The current activity gives the interface to the ListFragment.
I am going to develop an app with the following structure:
Search: Lets user search for articles and displays search results in a list
Article: Displays an article
UserList: Displays a list of articles the user has chosen to add to the list
UserListItem: An item that represents an article in the list mentioned above, and lets the user add custom information via some EditText-fields.
The Search, Article and UserList seem like they could be implemented as fragments. But what about the UserListItem? There will of course be multiple UserListItems on the screen at the same time, could it still be implemented as a fragment? If not, how should it be implemented?
I'm having some trouble grasping the whole fragment concept. It seems obvious how to use it in the standard scenario, i.e. Search-pane and Article-pane. But it's a bit unclear to me if it should/could be used in a scenario where you will have multiple instances of the same fragment displaying at the same time.
I haven't yet written any code, because I want to have the overall structure clear before I start, so I don't have to go back and change everything.
This might be a bit much if you are a beginner, but if you want to add searching capabilities in your application, consider creating a search interface.
From the documentation on Fragments:
You can think of a fragment as a modular section of an activity, which
has its own lifecycle, receives its own input events, and which you
can add or remove while the activity is running (sort of like a "sub
activity" that you can reuse in different activities).
That being said, there is a huge difference between incorporating a behavior in your screen's layout and wrapping that behavior in a Fragment. In your case, it really wouldn't make sense to wrap each list item in a Fragment as it would be ridiculously inefficient to instantiate a new Fragment for each item in your ListView. Representing each list item as a Fragment would give each row its own lifecycle, which is obviously not what you want. What you probably want to do instead is represent each list item in XML, and have the Fragment (or Activity) that holds your ListView manage these list items as necessary.
I would like to write a rather simple content application which displays a list of textual items (along with a small pic).
I have a standard menu in which each menu item represents a different category of textual items (news, sports, leisure etc.). Pressing a menu item will display a list of textual items of this category.
Now, having a separate ListActivity for each category seems like an overkill (or does it?..)
Naturally, it makes much more sense to use one ListActivity and replace the data of its adapter when each category is loaded.
My concern is when "back" is pressed. The adapter is loaded with items of the current category and now I need to display list of the previous category (and enable clicking on list items too...).
Since I have only one activity - I thought of backup and load mechanism in onPause() and onResume() functions as well as making some distinction whether these function are invoked as a result of a "new" event (menu item selected) or by a "back" press.
This seems very cumbersome for such a trivial usage... Am I missing something here?
Thanks, Rob
If the user hits the back button your Activity will likely get garbage collected.
If you properly start your activity from the menu with the different categories through an Intent, with passing the category etc. and then choosing the content in the onCreate method, you will get a new Instance of your Activity every time the user chooses a category, that will be destroyed after the user hits the back button.
This behaviour is perfectly fine. You don't have to cope with strange error cases and populating the list will take some time so the object creation time of the new ListActivity will be no problem. Try to program it as easy as possible for you and then test if there is a performance problem in the end of doing so.