Populate Android App with JSON data - android

I want to build an android app for a restaurant. I want it to display the menu in several activities using listviews. One for drinks, one for meals, one for deserts, etc. The menu comes from a json file which is being parsed in a splashscreen on app start.
Now i'm wondering what's the best way to populate the listviews and activities after parsing the json:
Populating the listviews from the splashscreen activity?
Storing the menu data in files and access them on the depending activity's oncreate()…?
Parsing a separate json file for each activity?
What is the best way in terms of performance, simplicity and effectiveness?

That would imply you have all you Acticities with their ListViews up & running when the splashscreen is starting. Probably not what you want.
You already said the JSON is stored in a file. You can parse that JSON in your splashscreen Activity, keep a global reference to it and let you Activities pick the part they need from that global class / JSONObject.
Tradeoff: Parse the complete JSON once and let all Activities retrieve the parts they need from it (this is basically 2.) at the 'risk' of parts of it never being used or split the JSON into several parts that are being loaded/parsed on demand by every Activity seperately, but having the overhead of doing file transactions in every Activity.
Either way, if the menu you're storing isn't immensely huge, the difference in performance will be minimal.
I'd go 2., load the whole file at startup, store the data globally and let every Activity make use of it.

Related

Android - Create and start Activities based on server response (JSON)

I have a JSON response from the server, like this:
{
"types": [
{
"type_name": "ACTIVITY_TITLE_1",
// ... other Activity realted data
},
{
"type_name": "ACTIVITY_TITLE_2",
// ... other Activity realted data
}
// ... and who knows how many more other type object...
]
}
I get this JSON from a background service, and after I saved that to a DB, my SplashScreen should generate activities from this JSON.
So I need to create a new Activity for every 'type' what I get from the server, and automatically start them one after the other.
Every Activity is exacty the same (so I instantiate the same class), just the content differs (what I get from the JSON).
The only problem is, that I don't know how many 'types' I'm gonna get, so how many Activity I need to create, and I can't use Fragments (what would be an easy solution), beacause I have to use Fragments in these Activites.
What is the best design approach for this?
From the first activity (kind of a long 'splash screen') I should start the ACTIVITY_TITLE_1 activity with startActivityForResult(), and when I'm done I start the ACTIVITY_TITLE_2 from that 'splash screen'?
Or I should start the ACTIVITY_TITLE_2 activity from the ACTIVITY_TITLE_1, and inside that ACTIVITY_TITLE_1 get the information somehow from the DB, that there are more activities based on the JSON array waiting for the start, and start the next one from there?
Neither looks too 'professional' solution for me... :D
EDIT:
I'm developing kind of a simple "dashboard", and I have new data from the server in every 10 minutes in a JSON form like above. So I only need to display the latest JSON response, which is a "blueprint" for activities, and show them automatically one after the other... In the example above, there will be 2 activities automatically alternating in a few seconds... But if I get a server response 30 minutes later, which contains 3 activites (types), than I need to generate and show 3 one after the other. Always based on the last JSON response.
So, I'm gonna save immediately to DB my JSON response, but in the next automatic "round", I'll have to show 3 activities now. So I need to generate and start activites what I create on the fly... :D
Ok . You don't need to create new activity foreach json response . Just hold one activity and than load the content depending on the response you get. Just from your splash screen which is an Activity/Fragment for itself send data to a new Activity you will generate . If you need to save all your responses hold a local database (Sqlite, Realm or whatever) and just change the content depending on the value that you need .

Session state of ListView

I am new to Android App development, working on an android app which populate a list of numbers, in a listview dynamically, depending on the choice of the user, but, the moment user closes the App, the items in the listview are lost. How can I maintain the state of the listview?
Examples with code would be highly appreciated.
When I open Activity A, it allows users to add friends, and this friend list is shown in the form of items of listview in the same Activity, however, when I move to Activity B, and then come back to Activity A, this friend list disappears. I need to make sure that this friend list should not be lost while moving between activities. Please help.
I think that for your purpose there are 3 main methods, i'll explain them from the easier to the most difficult (in my opinion).
Text File
A way to do this is to create two methods in a class:
one has to create the text file in the storage if it isn't created before and read that, the other has to append a String to a StringBuilder and write it on the previous text file.
For this method you need the uses-permission of reading and writing to storage.
This link can help you: http://developer.android.com/training/basics/data-storage/files.html
JSON (also XML)
With JSON file you can create a list of objects with your data that you can serialize when you update the list and deserialize when you want to read it. For this purpose you have to study JavaScript syntax or, at least, JSON one.
SQLite Database
Android SDK incorporate a class named SQLiteOpenHelper that you can extend to create a database inside your app.
This link can help you: http://developer.android.com/training/basics/data-storage/databases.html
There are also references saving methods but i think that aren't right for your purpose, they work betters to save something like preferences or single data like last login informations.
I went through your comment. I would personally suggest using SQLiteOpenHelper
You might need to understand the use of SQLite, hence the tutorial
Simple Flow. On your Activity 1 where person Add Friends save it to DB
Then refresh the List from the DB. So when you move to Activity 2 and come back again to Activity 1 your List will refresh from DB. Hence no loss of data as you want.
EDIT
As user wanted to know how to use the ListView with DB.
Following are my suggestion
Sai Geetha
Youtube

Using multiple static ArrayLists through a whole Android application

I am developing my first android Application and hoping to get some tips here.
I am getting a JSONObject from an url which then will be parsed in an ArrayList<MyObject>. The list will be used in multiple tabs and be filtered as needed for the tabs. The objects within these list can be modified by the user and the changes should be synchronized with the lists.
So, to speed up loading time I have created a class DataHolder as a singleton which contains 7 arraylist, based from the one JSONObject in different sorting order and filter criterion. The objects in these lists are references from the original list. Populating the lists works fine.
The lists will be used in different fragments and activities.
Now the problem: the second activity contains tabs with fragments. After initializing the fragment... all arraylists in the DataHolder counts 0! I have to save the JSONObject in SharedPreferences and populate it again to get the List. I can't load the url again because it is slowing down the app to much and using SharedPreferences is not an option (I think) because of the need to synchronized the Lists. I have read that using static variables is not the optimal solution, but it seems to be the easiest way :(
What can I do to solve this problem? Should I use Parcelable Objects and always pass the Lists around? Or maybe use SQLite? Or are there other approaches?
The SQLite way is definitively the correct approach, imho.
You should use the internal database to store such lists :
http://developer.android.com/guide/topics/data/data-storage.html#db
use static list for local operation .when user comes out of that screen(activity) save that changes into database.

Controlled Iteration through XML Data

Good afternoon,
I have a simplistic app (just learning) that reads some xml data from a mocked up file. The XML data is well formed into 6 categories and I use the SAX parser to read it. My app basically has two buttons prev & next. So when the app loads I'd like to see the first category of xml data. When the user presses next button...well then I'd like to see the next category of data etc. to the end. My question is how do I go back and forth through the data? Do I load it all into a data object with some form of sorting and iterate back and forth throught the object or do I add an atty field to a parent element and just search the xml for the requested atty and child data? I don't forsee the xml ever getting very large. Just trying to get more experienced input into how to go about synchronizing the data with the gui.
TIA
JB
There are many ways you could go about it. One that is generally a decent path is parse the XML into a data structure that can be used by an Adapter to create the view structures and return them so be shown. That will give you a good level of control over how your data looks and allow you to tie in to many different complex View structures pretty easily.
The data structure that you store it in also has many possibilities. Which ones work best would depend on your particular dataset generally.
Given what I know about your data an ArrayList seems like a straightforward approach. Create yourself a class that will hold all of the data about one category. Create objects of that class in your parser as you are pulling the data out of the XML file, each time you get to a new category add your object to an ArrayList. When your done you should have an List structure that has 1 category object(with all of its data) at each index.
Once you've got that set up make yourself an ArrayAdapter with your List. Override the getView() method to inflate your View objects and populate them with the data from your List.
This Adapter can then feed a parent View (ViewPager, ViewSwitcher, ListView etc...) These parent views will make it easy to iterate over your data structures (i.e. switching from one category to the next and back.)

How to organize functions/methods in android app?

In my application, when user presses Sync button (calls function onSynchronize()), I need to do the following activities:
form url for data synchronization based on user's preferences
download url
parse data received
for each item found in data download picture (another url)
update ListView with data downloaded and parsed
What is the best approach to split this activity between classes? Since steps 2-4 should be done in background (ASyncTask), and the same steps 1-4 will be used in the service (for automatic synchronization).
Should I put step 5 in onPostExecute of according ASyncTask? Or, should I put there steps 3-5? What is the most logical and clear approach?
First, you need an Adapter class for your ListView. Also you'll need a parser class(you can implement SAX or DOM if response is XML). Take a look at Lazy load of images in ListView to understand image downloading into ListView.
Your parser class can return an array of custom objects and then you'll supply it to listview via your custom adapter class.

Categories

Resources