Navigate between several ListView - android

I have a ListView that represents a list of folders and when I click one item, I want to load another list that shows the content of this folder. How can I link these views together and to be able to go back to the first one with the back button ?

Well, since you didn't provide a code in your question, I will try giving an answer in a descriptive manner.
You can use fragments to do this. Your base Activity's layout must have a fragment container which you will use to display your fragment containing the first ListView data. Once after you click on a cell, you call the constructor of the second ListView, and replace the current content of the fragment container with the newly created fragment.
You may implement a back feature by implementing an ArrayList in your Activity and appending the fragments into that array list as the user navigates through the list. onBack pressed you can call the top most fragment from that Array list and assign it to the fragment container.
This should work well, given there are not too many types of ListViews that you may want to implement.

Related

use of multiple fragments inside a fragment in android

I have a requirement in my application. my application has 6 fragments in a Navigation drawer each fragment represents single item in navigation drawer list.
But in one of the fragment i need to implement a form which will be spread across 5 views one at a time, can any one suggest me how to approach this in android.
Initially i thought of doing this using ViewPager , but i dont need horizontal swiping but need to go to next screen when clicked on a button.
Kindly advice me how to approach this.
Thanks & Regards.
Nagendra
You can create main fragment and sub-fragments. Inside the main fragment, show the sub-fragments by replacing them, store the data by creating an entity object here and pass it to the sub-fragments. Inside sub-fragments, pass the data to the main fragment entity using listeners.

Using object from one fragment to another fragment, android

I have two fragments in same activity class.
On first fragment I made a public object which have two lists inside it.
and populate using gson library from a json string.
then I showed first list on listview in first fragment.
Now I want to show second list on another fragment.
how can I get the same object in second fragment, so that I can use its second list.
Second fragment will open after clicking on a button which is in first fragment.
You probably could create whatever objects you need and are common for your fragments in your activity and access them from fragment by ((YourActivity)getActivity()).yourObject
Sorry that is not possible way you are trying to do.
Because it violates the OOPS principle that one child can not have multiple parent.
So how to do this.
1) Create a common fragment with listview.
2) From activity load data as you are loading.
3) And pass data to fragment and that you want show in first and second and so on...
4) No need of two listview
5) Just change the data for listvew

Android ListView in Fragments

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.

Handling fragment transaction in android.

Hi I am developing android application in which I am using one activity and two fragments. Consider same example which google explain like one list view and detail view. on click of list item we are rendering respective detail fragment.
So I learn how to do fragment transaction and i come up with two solutions. One which is standard way which google explain that make one interface and implement that interface into main activity. And do fragment transaction there inside main activity.
I tried with another way. when I click on list item inside click listener instead of calling interface I change fragment inside my list fragment only and its working fine.
So i want to know what is difference between those to methods. changing fragment from main activity and changing it from fragment only.
What kind of problem i will face if i implement with second method.i.e. changing from fragment only.
Need Help. Thank you.
What kind of problem i will face if i implement with second
method.i.e. changing from fragment only.
There isn't an actual problem, it's more of a design discussion. Using the second approach means you're making a very specific fragment, one that on a click on one of its rows will make a transaction with a specific fragment on a specific place of the holder activity. This would be a problem if you plan on reusing this fragment.
Suppose you have this ListFragment and you decided that it should be used in five other activities(with different data). Because it has a very precise action when clicking one of its rows, this fragment will always require the holder activity to have a specific container(where the transaction will be done) along the specific detail fragment with which it was initially used. The problem is that in each of those five activities you may want to use a different fragment when clicking a row of the ListFragment, this would require making changes to the class of the ListFragment.
Now suppose you have the same behavior with the interface approach. As the ListFragment doesn't know or care who handles that click event(as it passes it to whoever registers as the listener) you can simply put the ListFragment in the five activities with no problem(so no changes to it at all). In the interface method of the activity you could then implement the desired behavior with whatever fragment you want and in whatever container setup you want.

Advantage of ListFragment over a ListView or normal Fragment

I have an activity with a menu of items running down the left side that are textViews. when the user selects one of the textViews it will put a listView in the rest of the area of the activity taking up the other 2\3 of the screen. when the user touches a different textView on that left side menu of the page, it will open a different corresponding listView of items.
i was considering putting a large listview on the screen for this purpose. however one other way is instead of using a regular listView in the activity, i could put a list fragment in there and switch between fragments.
The third choice is to put a fragment in there and put a listView inside of that fragment. I have never used ListFragment before.
which would be the best plan considering that there will be no orientation change? the activity will be locked in the vertical / portrait orientation. this will be running on a tablet, not used on smaller devices like phones.
i don't know if i will need to use loaders, because the list will not be long and the contents of the list will be text only.
is there any advantage to using listFragment over the other choices?
The three choices for this activity:
activity with ListView on it
activity with ListFragment on it
activity with fragment on it that has ListView inside of theFragment`
A ListFragment is basically a slightly specialized Fragment which makes handling a ListView present in the layout of the Fragment easier by offering some convenience methods(like getListView() so you don't need to search for the widget yourself, a method to get the adapter of the ListView etc). If you need a Fragment with a ListView, use a ListFragment. So in the end it's about deciding between a ListView and a ListFragment.
Between the two options, taking in consideration your scenario, I would simply use a ListViewbecause it's plain simple. The need of a Loader isn't a problem as you could use the LoaderManager of the Activity for the ListView.
However, you didn't mention how the BACK button should be handled. If you want to offer the user the possibility of navigating back through his choices use a Listfragment to get that for free from the system.

Categories

Resources