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
Related
I would like to start by saying if you can think of a better title for this problem, feel free to change it since I have no clue how to explain this in a short way.
So here is my problem:
For the application I am trying to make I have these schedules, one schedule for today, and one for upcoming days. Both of these are a listview inside a fragment.
(I used those fragments to create tabs to seperate the two.)
Each game (let's call them games because calling them activities would be confusing) on the schedule has a status, and here is where the annoying part comes. I have been asked to check if any game has been started, and if so I need to disable the buttons to start any other game than the one that is already ongoing.
EDIT: The main problem is that I cannot use findViewById on the listview item because apparently it is a null object reference
I made a little "paint"ing to give you more of a graphical representation.
So long story short, I need a way to check the status inside of every listview item inside of the listview inside of the fragment inside the activity to use said status to disable every button except for the one of the ongoing game.
Little side note: If no games have been started yet, all buttons are enabled.
I hope this made sense.
If you want some code to go with this, feel free to ask but be warned, I am making this inside a testing app so a lot of useless testing data and sloppy code.
EDIT:
This is where I am stuck in a more clear example:
The start buttons are enabled but should be disabled.
Scrolling further down the list, there is a started 'game' and right below it, a game with the same status as in the previous picture where the button is disabled as it should be.
This is because the "isStartable" boolean in my code goes to false after the game with status "start" has passed and the following items are disabled.
When I scroll back up, it is how it should be, the items are all disabled but I need them to be like this when the listview gets filled. Any way to refresh the data in this listview and taking the "isStartable" boolean with it?
This is what it looks like after I scroll back up.
create a model class for your listview data items. Set a boolean variable in model class like isGameStarted. Set that variable as per your result.Then in your listview adapter, put a condition as below
if(isGameStarted){
holder.startButton.setEnable(true);
else
holder.startButton.setEnable(false);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So I want to create an app that has a list of things, but when you click on one of the things in the list, a whole new page is displayed showing information on the price of the object in the list.
This is list would have over 100 things on it, would need to have a different image beside each one, and would need to have the ability to add things into the list(because its going to be alphabetical).
I'm not really asking for the code on how to make this happen, but more the battle plan on how to make this happen, so like "Use a ListView here, and then here do this, etc".
Thanks in advance :)
It seems like you'll primarily be dealing with two activities, one for your list of items and one for displaying the details of the object.
Object List Activity
You'll probably want your activity to use a ListFragment to load all of the objects. You can then create your own adapter subclass of BaseAdapter and load all of your data into it so you can create each view for the ListFragment. Once you've created the adapter you can use the setListAdapter method to set the ListFragment's adapter and start loading the data you have in the adapter.
Adding New Items
If you want to add new items you will probably want to create a menu with onCreateOptionsMenu in your ListFragment with something like a plus button as the icon. When onOptionsItemSelected is called you can start a DialogFragment to give the user the option to add new items.
Once the user has added an item in the DialogFragment and save the data somewhere such as your SqliteDatabase. Once the DialogFragment has returns to the ListActivity you should update your adapter with the newly added item as well as call getListView().notifyDatasetChanged() in the ListFragment.
Object Detail Activity
This should be pretty simple. When the ListFragment gets a callback from onListItemClicked simply pass this data through an Intent and start a new Activity to handle displaying the details of the object clicked
You should use those things:
ListView
ArrayAdapter<>
FragmentActivity
Custom Dialog
Because you are going to have over 100 elements on it, it's good idea to let Android (ListView) handle memory stuff. I mean, it's going to reuse cell/arrow/view from the list to avoid memory abuse.
With ArrayAdapter you can handle the "add/update" listView and customize all you want. You should use another FragmentActivity/Dialog for new values for "add" process.
FragmentActivity is just a preference of me because compatibility with Android's version
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.
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.
I ran into the situation that I need a way to edit the data of list-view item from another activity. I can handle the click event on each item, then edit it on the fly. However, I still prefer to handle all the editing in a separate activity. My listview item is customized from BaseAdapter.
Here is my main page,
Each item within the ListView, contains two other TextView. When the I hit the menu setting, it will go to another activity where I can edit and update the information:
I'm currently having two solutions in mind. Either retrieving data from the previous activity and update the whole ListView (which I think it's kinda expensive in the case user just edit one item). Or I can just get rid of the ListView and create a bunch TextView in the xml, by doing this I can just reference to each TextView by their id. So my question is, which way is preferred in this case? Any suggestion would be greatly appreciated.
Your ListView is displaying Email, Name, Headline, etc? That should be a fixed XML layout with TextView entries, I think. ListView is for variable numbers of elements. You CAN implement it with a ListView, but I wouldn't.
However, your concern about updating the whole list being overkill, I wouldn't worry about that either. You're talking about 7-10 fields. The amount of time Android needs to run through its lifecycle and display everything will dwarf you updating a few fields.
You can use SharedPreferences for this. You can create a wrapper class through which you can access the preferences.Thats the usual way to go about solving these kind of problems. You can check this for details.
You can have it as a variable in your application class, so that you can access that in a global context.
Use text views instead. List View code has been optimized for large amounts of data only and not recommended for small data.