Android place view in for loop - android

I have a problem. I am parsing a JSON file from a website, and want to go through it using a for loop and display the content each JSON object contains. Each JSON object should correspond to one for loop cycle, and it should contain a header where a title and some information get displayed, and a ListView with more information about the JSON object. How would I do that the best way ?
The amount of JSON object is unknown and changes, so I assume all the views have to be created programmatically using a for loop.
It should look somewhat like this.

I'm not going to code that for you, but basically you could do the following:
Download JSON data with an AsyncTask
Parse JSON (you could GSON or Jackson) into a List<>
Use an ArrayAdapter or crate your own list adapter
Add the adapter to a ListView
Done

Related

Parsing JSON performance

I have json with some objects in it. And only one object from json i need. So which approach will be better for speed and performance - parse only this object or parse whole document instead?
If you want to show only the particular JSON Object which may be either inside a JSON Object or JSON array itself then you just parse that particular JSON Object instead of parsing whole document.
If you are not going to use rest of the data, you can just parse the item you only need.
In theory, just the object would get you a result faster with lesser work times. So just parse the object you need.

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.)

JSON object to listView

i am newbie to android, I wanted to implement dynamically JSON data to my android listView, I have only to objects in JSON file this are dealname and discount.
I checked all questions and tutorials but when i implemented those codes in application, neither one is running. I just wanted to ask, to implement such JSON data, should i need to parse data and convert into String array so simple listview work, or i need to implement custom listview?
Plz your suggestions will help to solve this assignment..
Thanks in advance.
Look at these two tutorials, In these Json object are displayed in Custom ListView, I think this is what you needed,
Android Putting Custom Objects in ListView
Populate Listview from JSON
For simple example no need of custom list view. you can work with simple list view.
Parse the JSON data retrieve the data
store them in a list and pass that list as an argument to the adapter
set the adapter to the list view.
You will need custom list adapter, and possibly custom list entry.
Assuming you have already parsed your JSON and have JSON Array,
this adapter will have to:
- provide amoubnt of entries in JSON arary
- create and populate entry views on demand for certain list entry identified by position. Keep in mind, that list entries are reusable ( and they should be reused to reduce amount of object alloaction)
If there is a lot of data in JSON, better approach would be to use GSON pull parser and some databinding , to create java objects istead of built in JSON parser

JSONObject to custom object conversion

I receive from a web service a list of json objects that may containing different keys. Eg one would have a title, a url and a list of picture urls, another one would have the same more a phone number... I need to keep these objects in memory but I'm not sure about the best way to do it.
1- I could keep the JSONObject as it is.
2- I could create an array of generic Item object with all the possible fields (most are common to all items) and for each instance set only the fields which have the corresponding keys in the json object, and leave the others null.
3- I could also create for each object a map with the keys contained by the item.
Any thoughts about the best method?
Thanks
Jul
I would look at Jackson or GSON for JSON object mapping.

Android Development - Using an ArrayList<String> vs an array of strings with ArrayAdapter

I am new to Android development, and I had a question about an excerpt I read in Commonsware's "The Busy Coder's Guide to Android Development". There is an example where it is constructing a class extending ListActivity, and showing the ability to remove and delete from the list via Context Menus. Prior to setting up an ArrayAdapter with the source of strings for the list, the text says to convert the String array to an ArrayList type, because "we want to be able to change the contents of the list on the fly, and that is much easier if you use an ArrayList rather than a ordinary String array"
I understand this concept IF we were using the ArrayList or String array to manipulate the list directly, but why does that matter if we're using the ArrayAdapter.add/insert/remove methods? Once the ArrayAdapter is created (and from what I understand from that .java file), it turns the source data into a List object whether it is coming from an array or ArrayList.
I tried using the normal array when creating the adapter and I got an error (as expected by what the text said), but I do not understand why. What is the difference between creating an ArrayAdapter with a string array vs an ArrayList...won't it turn it into the same List object anyway? Is there some kind of tie back to the original source variable when trying to add/insert/remove via the adapter?
Sorry for the long question, but thanks in advance.
You can change the original array/arraylist. Then you call notifyDataSetChanged() on the adapter and it updates the list with the new information. If you use an ArrayList, then you can use all of its normal operations to insert/remove data at an arbitary index that you cannot do with an ordinary array of Strings.
So the contents of the ListView is very much tied back to the original data source- the contents isn't fixed by the adapter at the time you call myListView.setAdapter().
However, the add, remove, insert etc. methods will cause an error if you try them with an array because the array can't be used like this. The ArrayAdapter will actually convert the String array into an AbstractList object, but which can't be modified.

Categories

Resources