将字符串数组添加到微调器条目时遇到问题

时间:2014-04-09 15:38:42

标签: android arrays android-spinner

我想要完成的是字符串数组名称[]是你在微调器中看到的,但我似乎无法弄清楚如何做到这一点。我试过看其他类似的问题,但我尝试的所有内容都会导致我的应用程序崩溃,说它无法实例化活动。下面,我已经包含了onPostExecute,我正在生成字符串数组,还有我的addListenerOnSpinnerSelection()方法。任何指导都会很棒。

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) {

                }
            });
        }

0 个答案:

没有答案