Android ListView in Fragments - android

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.

Related

Navigate between several ListView

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.

Best User Experience when navigating between 2 Lists (Fragment / Activity)

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.

How to notify list activity that one of its items has been updated in detail activity?

Imagine we want to design G+ application. It has a list activity/fragment and by click on an item then detail activity/fragment will be displayed.
My problem is, I don't understand if user hits plus button in detail activity and click on back button then how its counterpart item in list knows that user has clicked it in detail screen (since it shows number of plus oned as same as detail activity)?
Though it's bit late, thought of sharing my answer as it might help others who are coming across similar situation. Basically, you need to create an interface in the detail fragment class and implement it in the parent activity and use it to update the master list through notifyDataSetChanged().
You might want to refer to following SOF thread as an implmentation reference - Update ListView in Master/Detail after a form is saved

Android Fragments: Sharing Code with Dual UIs (stack-of-cards phone and side-by-side tablet)

I have a typical dual UI scenario - a list of categories, which when one is clicked, loads a category detail fragment containing a list of items in that category. On the phone, it's implemented as a stack-of-cards UI, opening up the details in a separate activity on top of the category list. On a tablet, it's the category list on the left, with the details on the right.
In the details pane, there's a button to add an item. The details fragment has an interface, required of Activities, with an onClickAddItem method, which should bring up a DialogFragment to ask you for the details of the item and add it when it returns.
The problem: both the tablet version's all-in-one Activity and the phone's standalone details Activity need the same onClickAddItem logic. There's a sinking feeling deep in my gut that the proper solution for this is to pull that logic out into yet another class, but the need to create several million files to do simple things in Android is slowly driving me insane, so I'm hoping there's another best practice I'm overlooking here. Thanks!
If your "add" button is in the detail fragment, there is no reason to handle the click event in the activity.
I think you should put the click event handling in your detail fragment.
Why do you want to keep all database access in the activity ? Make sure you're properly abstracting database access ( using a ContentProvider for example ) and don't be shy to use your abstraction wherever it makes sense. Adding an item using a ContentProvider should be as simple as:
getContentResolver().insert(myUri, myNewItemContentValues);
It you need to display a dialog, just get a reference to the current activity from the detail fragment, and use it to display your dialog.
If several fragments share the same functionality, you may need to write a simple helper class with some methods like:
public void showAddItemDialog(Activity activity)

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.

Categories

Resources