2

1. My question: How do I take my JSON data and parse it into an array that I can use in my spinner.

Below is my json data:

[["Mike Test 1"],["Mike Test 2"],["hello world"],["TEST MIKE 4"],["TEST MIKE 6"],["aliens,crazy stuff"],["Alien"],["american flags,flags"]]

My script calls a function that gets the data. I know that is working because I have toasted the return value. It should then loop through and assign the values to a new array that is used in the spinner.

Below is my java:

  Spinner areaspinner;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    JSONArray jsonArray;

    try {
    //SETTINGS AND METHOD THAT GETS THE DATA
        String spinnerContentType = "styles";
        String spinnerURL = "getStyles.php";
        String spinner_data =  DataCall.getJSON(spinnerURL,spinnerContentType); 

     //NEW JSONArray OBJECT
        jsonArray = new JSONArray(spinner_data);

        final String[] array_spinner = new String[jsonArray.length()]; 

        int show_total = jsonArray.length();
        //CHECK HOW MANY ITEMS ARE RETURNED
        Toast.makeText(flash_tattoo.this, show_total + "test", Toast.LENGTH_LONG).show();

        for (int i=0; i<jsonArray.length(); i++)
        {
            //LOOP AND ASSIGN TO ARRAY
            String styleValue = jsonArray.getJSONArray(0).getString(i); 
            array_spinner[i] = styleValue;

        }
        ArrayAdapter<String> adapter = 
            new ArrayAdapter<String> (this, 
                    android.R.layout.simple_spinner_item,array_spinner);

        adapter.setDropDownViewResource(R.layout.spinner_layout);
        areaspinner.setAdapter(adapter);

    }catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

This is my spinner layout:

<?xml version="1.0" encoding="utf-8"?>

<TextView 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/text1" 
  style="?android:attr/spinnerDropDownItemStyle" 
  android:singleLine="true" 
  android:layout_width="fill_parent" 
  android:layout_height="?android:attr/listPreferredItemHeight"
  android:textColor="#e70909"
/>
7
  • 1
    Do you see the options when replacing adapter.setDropDown.. with this in your code, adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); Commented Aug 18, 2011 at 4:54
  • Are you getting the data of Json in array_spinner. Commented Aug 18, 2011 at 5:03
  • @rochdev Changing the setdropdownviewresource did not help. Thanks though. Commented Aug 18, 2011 at 5:18
  • 1
    I see the word "areaspinner" only once in your code. Is that the correct name? Where are you instantiating that? Commented Aug 18, 2011 at 5:55
  • @Thunder Rabbit I added the areaspinner at the top of the code. Commented Aug 18, 2011 at 6:00

1 Answer 1

2

To me it looks like areaspinner is null.

To add a spinner you have something like this in your main.xml.

<Spinner android:id="@+id/mySpinner"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:prompt="@string/mySpinnerText" />

Then in onCreate()

Spinner sp = (Spinner) findViewById(R.id.mySpinner);
        ArrayAdapter<String> adapter = 
                new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, array_spinner);       
        adapter.setDropDownViewResource(R.layout.spinner_layout);
        sp.setAdapter(adapter);
Sign up to request clarification or add additional context in comments.

1 Comment

Your post and one of the comments help me figure out what the issue was. I was creating my spinner but I was not adding the spinner to a view since it was in my layout it was still coming up and that is what confused me. Thanks and +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.