0

What I'd like to accomplish is the String array names[] be what you see in the spinner, but I cannot seem to figure out how to do this. I have tried looking off of other similar questions, but everything I try results in a crash in my app, saying that it is unable to instantiate the activity. Below, I have included the onPostExecute, where I am generating the string array, and also my addListenerOnSpinnerSelection() method. Any guidance would be great.

protected void onPostExecute(JSONObject json) {
            try {
                // Get JSON Object
                JSONObject runes = json.getJSONObject(encodedId);

                // Get JSON Array node
                JSONArray rune = runes.getJSONArray("pages");

                // Loop through pages, page names stored in string array
                String[] name = new String[rune.length()];
                String curr;
                ArrayList<String> runePageNames = new ArrayList<String>();

                for(int i = 0; i < rune.length(); i++) {
                    JSONObject c = rune.getJSONObject(i);
                    name[i] = c.getString(TAG_NAME);
                    curr = c.getString(TAG_CURRENT);

                    if(curr.equals("true"))
                       name[i] = name[i] + " [Active]";
                    runePageNames.add(name[i]);

                    Log.i(".........", name[i]);
                }

                adapter = new ArrayAdapter(this,
                        android.R.layout.simple_spinner_dropdown_item,
                        runePageNames);

                addListenerOnSpinnerSelection();

                // Set TextView
                textName.setText(name[0]);

            } catch (JSONException e) {
                e.printStackTrace();
            }


    public void addListenerOnSpinnerSelection() {
            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    ((TextView) adapterView.getChildAt(0)).setTextColor(Color.parseColor("#C49246"));
                    Toast.makeText(adapterView.getContext(),
                            "Page Selected: " + adapterView.getItemAtPosition(i).toString(),
                            Toast.LENGTH_SHORT).show();

                    page = adapterView.getItemAtPosition(i).toString();
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });
        }
Nate
  • 392
  • 3
  • 5
  • 17
  • Are you setting an adapter to your Spinner? – joao2fast4u Apr 09 '14 at 16:07
  • @joao2fast4u I have edited my code to include an adapter, but I am not sure of its placement in my code and if it is correct. It is also giving an error "cannot resolve constructor" – Nate Apr 09 '14 at 16:29
  • Try to pass YourActivity.this instead of this in `adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,runePageNames);` – joao2fast4u Apr 09 '14 at 16:35
  • Maybe this will help you: http://stackoverflow.com/questions/2784081/android-create-spinner-programmatically-from-array – joao2fast4u Apr 09 '14 at 16:38
  • @joao2fast4u That cleared up the error just fine. Funny thing is I tried that earlier this morning and got nothing. I probably had another part of it wrong somewhere. Does the rest of this look good though as far as getting it displaying the data from the string array? – Nate Apr 09 '14 at 16:43
  • I'm also curious about the placement of my addListenerOnSpinnerSelection(); Is it fine where its currently at? – Nate Apr 09 '14 at 16:44
  • @joao2fast4u Also, with these changes, my app no longer crashes, but the list does not get populated by my string array. It still has the dummy entries from my strings.xml – Nate Apr 09 '14 at 16:49
  • You have to call spinner.setAdapter(adapter); before addListenerOnSpinnerSelection(); I think your addListenerOnSpinnerSelection(); is in the right place. – joao2fast4u Apr 09 '14 at 16:53
  • @joao2fast4u Is my setting the entries in the .xml file to dummy entries a problem or would they just be overwritten by my assigning the names array to the spinner? – Nate Apr 09 '14 at 17:10
  • When I click the spinner on my device after removing the android:entries line in my xml, I get this in my logcat: Attempted to finish an input event but the input even receiver has already been disposed. – Nate Apr 09 '14 at 17:17
  • call spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() directly. You don't need a method just to call another one. – joao2fast4u Apr 09 '14 at 17:43
  • In wich line are you getting that error message? See this: http://stackoverflow.com/questions/19832963/catching-an-event-when-spinner-drop-down-is-dismissed – joao2fast4u Apr 09 '14 at 17:48
  • @joao2fast4u That error is not giving a line. It is showing in the verbose log and just says W/InputEventReceiver: Attempted to finish an input even but.... – Nate Apr 09 '14 at 22:07

0 Answers0