How to create a N-level dynamically changing listview - android

im facing a problem in my project. What im trying to do is,
i need to parse a json reponse to a listview.
While clicking the row i will get some id , and now i need to call another webservice using that id and it should show another list .Suppose i clicked the First row ,ie Board "A". Then the next Listview should be the sublist of A . It might look like the following
A1,A2 etc may or maynot have submenus.
I can get the id of from the clicked row . And if the list is empty means no more submenus.
I dont know how to design this system? Does any one have any idea? Thanks in advance
PS: I will have to make different webservice call to get each submenu that depends on the ID im passing from the listview row click

A few ideas to get you started:
Instead of using ListView, take a look at GridView. ListView does not support more than one column (you have to support that manually) and GridView was introduced for this purpose.
Let's assume your initial list is shown in its own activity. This means you have an activity which queries the webservice on its onCreate() and then displays the results in the ListView/GridView when they are returned asynchronously from the webservice.
It will be convenient to hold the submenu in its own activity. This means you should create another activity for the submenu (maybe it can share code with the first one or even be derived from it to reduce code duplication). This activity will receive in its Intent an argument (take a look at Intent.putExtra and Intent.getExtra) which tells it which row was clicked (it should usually contain the String filter you are about to pass to the webservice). Once this activity is created, in its onCreate it should query the webservice much like the first activity and show its results in its own ListView/GridView.
To connect the two activities you'll need to catch the on click event in the first activity, figure out which row was clicked, and then create an Intent to show the 2nd activity, pass the extra and show it.
This approach will let the user dismiss the 2nd activity using the back key once they're finished with it. Once it's dismissed, the user will be returned to the 1st activity where they can click on a different row.

Related

Design pattern for editing list item

I have a very basic app that reads an array json file and displays it to a recycler view.
I want to be able to modify one of these items but can think of multiple ways, none of which feel right.
It cannot be a new problem so I am asking if there is design pattern for just this case.
Currently i have my main Activity that shows the list.
If i press a list item, i navigate to an activity to show the item detail.
There is an edit button on this activity that leads to an Activity to edit the list item.
Once edited, it needs to be saved to the json file, and then calls the main activity that re-reads the json file and displays the items.
This feels very inefficient.
Appreciate any advice on how to improve this, thanks!
I think you can change the list in the MainActivity by calling the adapter's notifyDataSetChanged(), and save to JSON file in onPause() or onStop() method of the MainActivity. Because to my knowledge, I/O takes time therefore need to be minimized

listfragment android layout

I'm trying to create a layout in my fragment that allows a user to enter their workout. The fragment has an ArrayList of sets, and each set consists of an exercise name, sets/reps, and rest time. I can only populate so many sets at the beginning, and want to add a button at the bottom 'add exercise' in case the user wants more exercises than what the loaded layout allows...example..
Ex #1
sets/reps
rest time
Ex#2
sets/reps
rest time
[add exercise]
I tried using a listfragment and an adapter, but im not sure how I can withdraw the data that is entered into the edit text fields and save them for later use. Any help is greatly appreciated, thanks in advance.
Using adapter is a good idea.
Here is one way to achieve your goal:
When person clicks on add exercise button, you should get the new exercise information from the user. You can use Dialog or another activity-screen for that.
Once you got the info(sets/reps and rest time). Go and update the ArrayList that populates the data in the adapter. And when you done call: Adapter.notifyDataSetChanged() this will make the adapter to invalidate the view based on the changes in the data(ArrayList in your case).

How can I efficiently transfer checked items from a listview to another activity when data is supplied by a Content Provider

The title doesn't really cover it all. It's basically the following use case:
Select a bunch of items in a listview
Move to the next activity
Show the selected items in this new activity in a listview
If the user decides to move to the previous section the same items should still be checked
If I were using POJOs I could simply add an OnItemClickListener to the ListView and add the items to an array each time user clicks an item and ship this array to the new Activity. However the ListView is populated using a Loader which gets the data from a ContentProvider and puts it in a SimpleCursorAdapter.
In the current state I can think of a few possible solutions:
1) Add an extra column to the relevant table of the objects in the ListView. Each time the user selects an item this column is updated to indicate it is selected.
This has a few advantages:
Once I have moved to the next Activity I can simply query for all selected items.
When moving to the previous activity I can use this column to show a selected view.
But also a few disadvantages:
Requires an update each time an item is clicked, triggering the loader.
Requires a custom adapter which uses the state of the new column to decide whether it should or should not be shown as checked. Possibly creating a delay between clicking an item and actually showing it as checked.
The default check options in the ListView will be unusable
2) Track the IDs of checked items (using OnItemClickedListener or getCheckedItemIds) and pass them to the next Activity. In the new activity I then create a selection argument of "ID = ? OR " repeating N times (and excluding the last OR) and use the given array as the selection arguments.
Advantages:
No need to keep updating the database.
No extra columns in the database.
Item checking has no extra delay.
Default check options from the listview still usable
Disadvantages:
Moving to the previous activity is now harder. I could return the list of selected item IDs, but the listview only has the option setItemChecked which takes a position. So I'd have to iterate over the entire adapter to find the positions of the items and set them as checked.
I'm probably capable of implementing them without any hassle. At the moment I'm gravitating towards the second option.
So ultimately I have the following questions:
Is there an easier way to do this in Android.
What would be a good way to recheck the items (see the disadvantage in the second suggestion and if there's no better way to do it).
This ListView will also get a search function which will probably again make it a bit harder because if I'm not mistaken filtering resets it every time. So I'd also have to recheck items every time (ideally during filtering).
Your disadvantage "Requires an update each time an item is clicked, triggering the loader." is not quite true it will trigger only if your ContentProvider calls notifyChange you could have it not to call notifyChange in some specific cases
and pressing back by going to the previous activity shouldn't be that hard only if you explicitly call finish() on your activity, otherwise it should be able to save its state onSaveInstanceState
Either way i would use a third approach and simply wouldn't use a second activity but would keep everything inside a single activity use pearhaps two Fragments one for the initial check list and the second one for your second preselected list and would use callbacks and let the activity manage all,is that possible for your use case ?

Changing an activities setContentView() in Android

I just have a question regarding changing an Activities layout.
Basicly my problem is:
I have a list view populated with a series of Strings.
I have an OnItemClick event assigned to the ListView,
When the user clicks an item in the list view, I want the current layout to disapear and an image to take its place.
From reading other posts, I understand that the recommended way is to set a separate activity for each item in the ListView, however, seeming as all the activity is doing is displaying an image I think it would be a waste of effort to set up a separate activity for each item in the ListView...
Could anyone give me some help on this?
thanksin advance.
Shaw
You really should create a new activity (ar a new fragment if you have space, but this is another question) which displays the image, for at least the following reasons
it is very, very simple to code and mantain such a solution. What if tomorrow you want to add 2 buttons and some text and maybe a menu for that image? you have your brand new activity to edit and upgrade without risking to damage your list activity.
it is more user friendly. If the user presses back when the image is shown, with 2 activities he will be back to the list, with your solution he will go back BEFORE the list, and this is not what he'd espect
remember this piece of advice: 1 activity = 1 simple task or interaction with user.
list + image display = 2 activities (or fragments)
PS: you do not need to define a different activity for each list item, just pass with the intent to the "ImageActivity" and specify there which image to show!
EDIT 2: to pass to the next activity your current selection, just use the putExtra(String key, T value) (T may vary, check documentation) method of Intent class.
example: intent.putExtra("imgCodeSelected", index) where index is fetched by the onItemClick event. You can put as many extras as you want as long as they have different keys.

What is the best way to do multiple listviews in android?

i am writing a software that i have to drill down on content a lot. For example when the program starts a listview is displayed. When user clicks on an item, then a second listview must be displayed. For example:
Select Continent > Select Country > Select State > Select City > Select Address
What is the best way to do this (less memory, faster, easier to code etc)? To create multiple listviews with multiple adapters? Or 1 listview with multiple Adapters? Lists are loaded from an external XML File.
So far i am creating a new adapter and setting it to the listview. How do i create a second listview and after clicking on 1st listview displaying the second one, with animation. Any examples?
Extend my class to ListActivity or Activity?
I would make multiple Activities for this. The first activity to display Continent list, the second the Country list, Third State list, etc...
When the user than click the back button. It will go back to the previous activity (list). Even the scrollstate would be remembered.
This will also add the OS animation between the activities. The code will also be separated and memory freed when closing the list activity.
Sending a value from one activity to another ex CountryCode tot the StateListActivty is done by setting a intent.putExtra("CountryCode", countryCode);
A second approach would be to use a ViewFlipper. Adding each listview as a child. And than setting a custom animation on the show-next and show-previous actions.
Note: Using multiple activities may use abit more memory than the ViewFlipper approach.
I would use separate activities for each list but with only one List Adapter class to be shared by all so you can retain consistency in terms of how the lists look + easy to maintain code. You can use a bundle to pass info from one activity to another.
Another thought: is the info you are referring to part of "settings" info? By that, I mean, is it info the user would put in once into your app, or would they put it in almost everytime they use the app (because each time the info would be different)? If it's an "one-off" type of info, you would be better off using a PreferenceActivity.

Categories

Resources