2014-04-10 36 views
0

下面我附加了我的代碼,試圖將我的字符串數組名稱添加到微調器作爲選項。截至目前,我沒有收到任何數據,我真的不知道我做錯了什麼。我瀏覽過這個網站上的其他類似問題,以及使用Google,並且已經空了。任何人都可以給我一些指導嗎?由於Android字符串數組填充微調器

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(); 
      } 
     } 
    } 
} 

回答

0

調用addListenerOnSpinnerSelection();方法設置適配器爲微調前,試試這個..

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

這樣做。非常感謝。 – Nate

+0

@Nate很高興幫助。快樂編碼。 – Hariharan