i am trying to select an item using Spinners. I have a JSON file stored in asset Folder. I want to select an item from spinner and then get its numeric value stored in my JSON File. and then store this numeric value in database. Please tell me how can i implement this
Best Regards
You need an
Adapterfor yourSpinner. See here http://developer.android.com/resources/tutorials/views/hello-spinner.htmlThat adapter will hold the values you select using your spinner.
If these are some fixed values then ArrayAdapter can do. You can fill it with values like follows:
After this you will have a spinner populated with
mValues. (Alternatively, you can useArrayAdapter.createFromResource).Then, you will have to read your assets file using something like
For assets see here http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/content/ReadAsset.html
You’ll probably need to parse the input stream using JSON parser. I believe Android has a JSON parser class. See for example here http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/
Oh, yes, as posted in the answer below by Anil it’s enough to parse the JSON file only once.
Then you need to store the value you found for your spinner element in the database. This gets long to to explain. You can see how to create and use databases in Android docs.