簡體   English   中英

Android使用字符串數組填充微調器

[英]Android populate spinner with string array

下面,我附上了代碼,試圖將我的字符串數組,名稱添加到微調器中作為選項。 到現在為止,我什么都沒有填充數組,而且我真的不確定自己在做什么錯。 我在這個網站上以及使用Google的過程中,也查看了其他類似的問題,但都空着。 誰能給我一些指導? 謝謝

public class RunesActivity extends Activity {

    public static String page;
    TextView textName;
    Spinner spinner;
    ArrayAdapter<String> adapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rune_activity);
        GetRunes getRunes = new GetRunes();
        getRunes.execute();
        spinner = (Spinner) findViewById(R.id.rune_selector);
    }

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

            }
        });
    }
    class GetRunes extends AsyncTask<String, String, JSONObject> {

        private String api_key="d96236d2-6ee3-4cfd-afa7-f41bdbc11128";
        String region = MainActivity.region.toLowerCase();
        String id = StatsActivity.sumID;
        String encodedKey = null;
        String encodedRegion = null;
        String encodedId = null;
        String url = null;


        // JSON Node Names
        String TAG_NAME = "name";
        String TAG_CURRENT = "current";
        String TAG_SLOTS = "slots";
        String TAG_RUNEID = "runeId";
        String TAG_RUNESLOTID = "runeSlotId";

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            try {

                // Assign views
                textName = (TextView) findViewById(R.id.name);

                // Encode URL variables
                encodedId = URLEncoder.encode(id, "UTF-8");
                encodedKey = URLEncoder.encode(api_key, "UTF-8");
                encodedRegion = URLEncoder.encode(region, "UTF-8");

                url = "http://prod.api.pvp.net/api/lol/" + region + "/v1.4/summoner/" + id + "/runes?api_key=" + api_key;
                Log.i("..........", url);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }

        @Override
        protected JSONObject doInBackground(String... arg0) {
            JSONParser jParser = new JSONParser();

            // Get JSON from URL
            JSONObject json = jParser.getJSONFromUrl(url);
            Log.i("............", "" + json);
            return json;
        }

        @Override
        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(RunesActivity.this,
                        android.R.layout.simple_spinner_dropdown_item,
                        runePageNames);

                addListenerOnSpinnerSelection();

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

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

嘗試這個..

在調用addListenerOnSpinnerSelection();之前addListenerOnSpinnerSelection(); 該微調器的方法集適配器。

adapter = new ArrayAdapter(RunesActivity.this,
                    android.R.layout.simple_spinner_dropdown_item,
                    runePageNames);
spinner.setAdapter(adapter );
addListenerOnSpinnerSelection();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM