-1

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

Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193
  • this isn't a good question for this site in my opinion. We are not code / suggestions dispensers, we are here to help you fix your problem or give suggestions provided that you show quite an effort before spending our time. We usually enjoy help people to resolve their issues, but please prove us that you've done your part of the work first. – STT LCU Feb 24 '12 at 08:38
  • well, i have created a JSON File instead of linking values to the spinner items. So i was just asking clues, how to link JSON file with spinner. – Muhammad Umar Feb 24 '12 at 08:56
  • did you already try something? did you get any errors? this is what I personally like to see in a question. – STT LCU Feb 24 '12 at 09:15
  • Well, i don't know how to make inputstream of file i am taking from asset to Jstring – Muhammad Umar Feb 24 '12 at 09:20

2 Answers2

1

You need an Adapter for your Spinner. See here http://developer.android.com/resources/tutorials/views/hello-spinner.html

That 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:

public YourActivity extends Activity {
...
private String[] mValues = {"One", "Two", "Three"};
private Spinner mSpinner;
public void onCreate(Bundle bundle) {
...
mSpinner = findViewById(R.id.yourspinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
   this, android.R.layout.simple_spinner_item, mValues);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
...
}

After this you will have a spinner populated with mValues. (Alternatively, you can use ArrayAdapter.createFromResource).

Then, you will have to read your assets file using something like

InputStream is = getAssets().open("your_asset.txt");

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.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • The problem i am facing is. How will i convert Inputstream into Jstring? – Muhammad Umar Feb 24 '12 at 09:19
  • See http://stackoverflow.com/questions/9135976/json-in-android there's a method convertStreamToString() will it do? – Alexander Kulyakhtin Feb 24 '12 at 09:51
  • Yes, i have done everything thanks. Just one last thing. I am confused about parsing again and again. I am using three activities. If i parce the JSON file in main activity can i use it in other activities or i have to Parce it in every activity? – Muhammad Umar Feb 27 '12 at 04:32
  • You don't have to parse it more than once. You can make your Application class, create a field in there holding your parsed file then if that field is null you parse, otherwise you just use that field. You will have to register your Application class in the Manifest. But of course you can parse every time for each activity. If hour json is small and if it's easier that way why not – Alexander Kulyakhtin Feb 27 '12 at 09:55
0

Follow the following steps.

  1. parse the json file. if you don't know how to parse json file then learn first it.
  2. when you select a item from Spinner, then you have the selected item. Search this item in parsed json file and pick up the corresponding numeric values.
  3. Store this numeric value in the database. if you don't know how to work with database, then learn database also.
  4. Do for each selected item step 2 and step 3.

we parse json file first because we do not need to parse it again and again for searching the selected element.

Anil
  • 153
  • 1
  • 6