JSON object to listView - android

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

Related

How to put data to spinner on another layout?

How can I to put data to spinner which is on another layout?
I am newby on android, and now I have problem with spinners.
I have data at first class with his layout. And I want to put that data to other spinner which is on another layout. What I need to do? Create new Class or What?
Please help! :)
Data is- Array
You should put your data where it is accessible from everywhere.
so please put your data in database using SQLite or ORMs that help you work with Native database like SugarORM, ORMLite and Realm.
another way is to apply singleton pattern to store your data in a different class.
You said you have an Array
Then make Array static
eg:- public static ArrayList<> new_array = new ArrayList<>;
now call this array any where in the activity
you will get data
there is many to manage array data.
- create arraylist and use that data anywhere in the activity.
- using database also you can user.
- using Read & writing arrays of Parcelable objects
- using global application class ....etc

Android place view in for loop

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

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

Parse JSON file and display an ImageButton and TextView for each entry

I have a JSON file that I want to parse. For each entry in the JSON file, I want to display an ImageButton (this will be an icon) and a TextView (a title). I'm just wondering what the best way of doing this is..
I DO NOT want to make a listview.. it is more of a grid view. I want 2-3 icons per row (so 2-3 columns) kind of like apple's bookshelf
I've started off with..
json = JSONfunctions.getJSONfromSDCard("/sdcard link to the file") //this gets the json file
JSONArray entries = json.getJSONArray("entries");
ArrayList<String> alTitle = new ArrayList<String>();
for(int i=0;i<entries.length();i++){
JSONObject e = entries.getJSONObject(i);
..//create arraylist to store entries?
//alTitle.add(e.getString("title"));
}
//create ImageButton and TextView?
You can use a listview inside this activity or a ListActivity. Then use a list adapter to create a layout for each list item.
There are samples supplied in the SDK for this to get you started. You will find these in the android-sdk folder wherever you have installed this on your PC.
There are also many tutorials on the internet so I cannont list them all, but here are a few to get you started. These are the top google searches.
Android Series: Custom ListView items and adapters
Android: Dealing with ListActivities, customized ListAdapters and custom-designed items
UPDATE
Sorry I missed the specification of using a GridView. The officaial GridView documentation includes a full sample The same idea applies using an adapter you can compare the different type of view in the Hello Views section of the official documentation
Here is a full example, you will need to add any additional controll into your adapter. Again, there are samples in the sdk itself and these are invaluable to Android developers.
I recommend looking into the GSON library for Android. It's supposedly a lot faster and easier to use than org.json in the stock SDK. I've also ran into bugs with the stock json library.
Basically it adds an interface similar to Java's serializables.
http://code.google.com/p/google-gson/
User Guide: https://sites.google.com/site/gson/gson-user-guide
You can parse the JSON, create an ArrayList then pass that ArrayList to an ArrayAdapter.
Use a custom implementation of ArrayAdapter as mentioned here
android, how to add ListView items from XML?
Create a layout XML with an icon and a TextView and deflate it in the getView method of your ArraAdapter implementation. Populate the values and you are done.

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