I just want to ask that how to load this type of JSON data from local asset folder in android spinners, I know most parts like loading the data and all that but I am not able to iterate through the JSON
{"india":["delhi","mumbai"],"usa":["newyork","california"]}
its a short list it has more data so just tell me how to iterate in this case btw i have two spinners first should be loaded with country names and next with respective cities of that country and the second spinner should be loaded based on the selected country from first spinner..
Use the following code:
JsonArray array = [your complete json];
for(int ss= 0;ss<array.size();ss++){
JsonObject object = array.getJsonObject(ss);
JSONArray ja = object.getJSONArray("india");
//do whatever you want to do with ja
}
this should depend on how you have defined your adapter and how you are calling this json data.
what I would personaly do is to use gson to parse this data into a POJO and store the results in an arraylist then use the adapter to loop through the arraylist
you can checkout my github repo for an example of a spinner https://github.com/AustineGwa/android-spinner2
Related
I have a raw json file like this
{"_id":707860,"name":"Hurzuf","country":"UA","coord":{"lon":34.283333,"lat":44.549999}}
{"_id":519188,"name":"Novinki","country":"RU","coord":{"lon":37.666668,"lat":55.683334}}
{"_id":1283378,"name":"Gorkhā","country":"NP","coord":{"lon":84.633331,"lat":28}}
{"_id":1270260,"name":"State of Haryāna","country":"IN","coord":{"lon":76,"lat":29}}
{"_id":708546,"name":"Holubynka","country":"UA","coord":{"lon":33.900002,"lat":44.599998}}
{"_id":1283710,"name":"Bāgmatī Zone","country":"NP","coord":{"lon":85.416664,"lat":28}}
{"_id":529334,"name":"Mar’ina Roshcha","country":"RU","coord":{"lon":37.611111,"lat":55.796391}}
{"_id":1269750,"name":"Republic of India","country":"IN","coord":{"lon":77,"lat":20}}
{"_id":1283240,"name":"Kathmandu","country":"NP","coord":{"lon":85.316666,"lat":27.716667}}
{"_id":703363,"name":"Laspi","country":"UA","coord":{"lon":33.733334,"lat":44.416668}}
It is NOT an JSON array - it is a huge list of JSON objects. Here Populate the spinner from JSON In Android, here Convert json array to list of json(raw) strings here android how to convert json array to string array or even here https://developer.android.com/guide/topics/ui/controls/spinner.html it is assumed that I have a JSON array or static data. I'd like to have two spinners in which one is for country and second one is for city. Let's assume I have list of countries so it will be a static data. I'd like the second spinner to dynamically load from raw json file all cities that corresponds to selected city in first spinner. I think that I can handle spinners thing but how can I load data from raw json file into spinner? Do I need to modify this raw file so that it will be a json array and then do something like this:
JSONArray jsonArray = new JSONArray(arraytitle);
List<String> list = new ArrayList<String());
for(int i = 0 ; i < jsonArray.length();i++){
list.add(jsonArray.getJSONObject(i));
}
EDIT
I found this answer https://stackoverflow.com/a/26780581/4671908 - if I will succeed I will post an answer
So you have two questions:
How to update the second Spinner dynamically when the first Spinner is updated
Three thing you need to do:
Implement AdapterView.OnItemSelectedListener
Update the backed data set inside the adapter of the second Spinner. In your case it would be a list of city. (To do this you need to create your own adapter extended from ArrayAdapter)
Call adapter.notifyDataSetChanged()
How to use the JSON
There are several solutions there. But it would be hard for others to provide the one that would suit your case the best without knowing any detail of the requirement.
Just remember one thing when you face design problem:
Do the simplest thing first.
I wish to get an array from the Parse cloud. After tapping on an item in a list (which contains the ParseObject reference), the item will open a new activity and from this I want to get the array list stored in the latLngPoints key in Parse. I have googled and only found the following solution but it does not work:
ArrayList<ParseGeoPoint> parseList = (ArrayList<ParseGeoPoint>) getIntent().getExtras().getParcelableArrayList("latLngPoints");
Is it possible to even do it this way, or will I have to create a Parse object and create a request to get the information I need?
ParseObjects are not parcelable. You can pass an array list of longs that represent lat and long
how to retrieve all images in MySQL to android
really I can retrieve data like (names,ages,....)
but really I can't retrieve all images
whereas I have a table in MySQL contain
(name , age,Image)
thanks for any guide and any help
i'm not sure this will helpful or not but you cant try this. http://www.androidbegin.com/tutorial/android-json-parse-images-and-texts-tutorial/
Probably you should consider to store the images in the local storage (instead of in a binary format, which is how it seems you are doing it, from your question), and in your database just store the file location. This way you shouldn't have problems converting the data from and to the database, and the database size wouldn't grow too much.
Then in your list view's adapter, you'll only need to retrieve the image's location and load it into the view you want to display.
EDIT
As I understood, you are sending the data to the Android device via Json. So let's assume you are sending a Json array like this one:
[{"username":"John", "userimage":"http://example.com/images/imagejohn.jpg"},
{"username":"Michael", "userimage":"http://example.com/images/imagemike.jpg"},
{"username":"Sophia", "userimage":"http://example.com/images/imagesophia.jpg"}]
So what you need is to read the JSON contents to an Array and fill it in an adapter. As I mentioned in the comments, you should take a look to the answer here: https://stackoverflow.com/a/8113337/1991053
Basically, you can read the JSON Array and put it into the ListView's Array Adapter, in the example above, you can retrieve each element like this:
JSONArray jsonArray = new JSONArray(input) // input is the JSON string you recieve
for(int i = 0; i < jsonArray.length(); i++) {
String userName = jsonArray.optJSONObject(i).getString("username");
String imageURL = jsonArray.optJSONObject(i).getString("userimage");
// Here you can do whatever you want with the values
// like loading an imageview with the url's data
// or putting the values in an ArrayList for filling up the ListView adapter
}
Of course you can modifiy this to fit in whatever the JSON structure you are using for sending the data, just use JSONObjects and JSONArray for this. Take a look to the documentation here: JSONArray, JSONObject
If you want more control over the JSON data (like serialize or deserialize the data), take a look to Jackson or Gson. There are plenty of examples around to understand how they work.
But I think your best bet is to use the adapter shown Here. Also, for loading up images in a ListView from a URL, you can use this library: Lazy List.
Hope this helps you for a start. Best regards.
I am trying to create an array like this ["stringone","stringtwo"] and pass it to the webserver. When I tried doing making it with a string array like String[]={"stringone","stringtwo"] it passed in something weird {"userids":"[Ljava.lang.String;#406fe4b8"} how should I be constructing my JSON array if not by using string arrays?
Thanks
If you want to create JSONArray from List or array, you can use constructor, which takes Collection:
String[] data = {"stringone", "stringtwo"};
JSONArray json = new JSONArray(Arrays.asList(data));
The easiest way is to create a JSONArray object and use the put method(s) to add any Strings you want. To output the result, just use the toString() method.
I'm using JSON in my app and I have a button "RSS", after clicking on which I want to see the RSS feed. While logging in, I also use JSON, but everything is done in background and the next view does not depend on JSON object. In LogCat I can see something like this {"response":{"#attributes":{"count":"4","all_results_count":"4","page":"1"},"news":[{"content_id":"43366","date_added":"04-01-2010","content_title":"New News","content_data":"mika"},{"content_id":"111443","date_added":"04-11-2008","content_title"..... But how can I actually display this on Android's screen?
Use JSONTokener to parse the JSON string.
string json = getYourFeed() // some method to retrieve the json response.
JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
int count = JSONObject response = object.getJSONObject("response").getJSONObject("#attributes").getInt("count");
JSONArray array = object.getJSONObject("response").getJSONArray("news");
for (int i=0; i<count; i++) {
JSONObject newsItem = array.getJSONObject(i);
Log.d("RSSReader", newsItem.getString("content_title");
}
use the get... methods of JSONObject to retrieve the rest the same way.
Update, based on your comment: I would start it simple, and then add more complexity as you get a feel for these controls. Create a String[] array with your news titles and add it to the list using an ArrayList adapter. It's very easy to use. Add an OnItemClickListener that shows a Toast with the full content.
Then, you can move to a SimpleAdapter version with a multiple columns ListView and perhaps a TabActivity that shows the full news.
http://ykyuen.wordpress.com/2010/01/03/android-simple-listview-using-simpleadapter/
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
What code are you using to get those outputs?
I would parse it with something like a 'SAXParser' and Display it using an 'ListView'...