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.
Related
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
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 want to group all contact in my app by using the first character of contact name. The result looks like deafault Contact Book in Android Phone.
I don't know the name of API which can solve my problem.
Could you give me the name of it?
Picture figures: image
You can use the an object of TreeSet class to sort strings as shown below:
TreeSet mySet = new TreeSet();
mySet.add("java");
mySet.add("C");
mySet.add("Pascal");
mySet.add("ruby");
Log.d("TAG",mySet);//output here will be C,java,Pascal,ruby,
//Now our task is to fetch strings from the sorted strings in `mySet` object
String[] names= mySet.toArray(new String[mySet.size()]);
//Now you will have sorted names in the names[] array
So, as commented if you are able to fetch string/names from your contacts, then you can use the above concept by creating an object of type TreeSet and adding all your contacts to this object and converting it to an array of strings as shown above.
I'm sure this one definitely helps.
I have a doubt. I have 3 array list dynamic values. I need to display these dynamic values in a listview. can someone please tell me how can i achieve this.
I have name[] array, status[] array and image[] array. I need to dynamically display the values in listview in a android sample
This is what i have:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,NameList);
In this i am able to display i am able to display all the names in NameList[].
An array list only accepts an array of values, so as you see, you can only pass in the names. You have two options,
The simple option is to create a compound object, say Person, that has a name, status, and image. Then you create a Person[], and create the array list on that, and pass it into the array adapter. You must implement Person.toString() to print out the Person object as you'd like.
If you need to be able to lay out the Person fields in a more flexible way then you could simply by implementing toString() on Person, you need to create a custom list adapter. You can take a look at this post to get you started,
Categorise the listview