2017-06-13 149 views
0

我正在使用我自己製作的一些API來開發Android應用程序。我差不多完成了,但我無法找到將數據放入我的動態微調器內的異步任務(獲取我的json數據的人)內部的方式。如何將我的異步任務數據放入微調器?

這裏是我的代碼:

String example; 

static final String API_URL2 = "https://xxxxxxxx.xx"; 
//Start Used for spinner with different value and display 
String[] textfordropdown = { "A", 
     "B", 
     "C", 
}; 
String[] valueofdropdowtext = 
     { "1", 
       "2", 
       "3", 
     }; 
Spinner spinnerdynamic; 

OnCreate中我已經打電話給我的任務異步看看,如果我得到這麼顯示的東西是它的工作原理:

new GetList().execute(); 

,我也是在這裏創建我的微調

spinnerdynamic = (Spinner)findViewById(R.id.dynamic_spinner); 
ArrayAdapter<String> adapter1 = 
     new ArrayAdapter<String>(MainActivity.this, 
android.R.layout.simple_spinner_item, textfordropdown); 
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
spinnerdynamic.setAdapter(adapter1); 
spinnerdynamic.setOnItemSelectedListener(onItemSelectedListener1); 

下面是函數onItemSelectedListener1:

OnItemSelectedListener onItemSelectedListener1 = 
     new OnItemSelectedListener(){ 

      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, 
             int position, long id) { 
       example = String.valueOf(valueofdropdowtext[position]); 
      } 

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

     }; 

在這裏,我的任務異步:

class GetList extends AsyncTask<Void, Void, String> { 


    protected void onPreExecute() { 
    } 

    protected String doInBackground(Void... urls) { 
     try { 
      URL url = new URL(API_URL2); 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
      try { 
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 
       StringBuilder stringBuilder = new StringBuilder(); 
       String line; 
       while ((line = bufferedReader.readLine()) != null) { 
        stringBuilder.append(line).append("\n"); 
       } 
       bufferedReader.close(); 
       return stringBuilder.toString(); 
      } finally { 
       urlConnection.disconnect(); 
      } 
     } catch (Exception e) { 
      Log.e("ERROR", e.getMessage(), e); 
      return null; 
     } 
    } 

    protected void onPostExecute(String response) { 
     if (response == null) { 
      response = "Une erreur c'est produite"; 
     } 

     Log.i("INFO", response); 

     try { 
      JSONObject object = (JSONObject) new JSONTokener(response).nextValue(); 
      JSONArray prestationlist = object.getJSONArray("WhatIWant"); 

      //Permet de compter le nombre d'éléments dans le json array 
      int arrSize = prestationlist.length(); 

      ArrayList<String> value = new ArrayList<String>(arrSize); 
      ArrayList<String> name = new ArrayList<String>(arrSize); 

      for(int i=0;i<arrSize;i++) { 
       object = prestationlist.getJSONObject(i); 
       value.add(object.getString("Value")); 
       name.add(object.getString("Text")); 
       //Here i've made some display to see if it works, i get the data. 
       responseView.setText(value.toString()); 
      } 

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

因此,如何能我把我的數據的String [] textfordropdown或valueofdropdowntext裏面?

非常感謝!

+0

請參閱[這個答案](https://stackoverflow.com/questions/7370107/update-city-spinner-with-notifydatasetchanged-after-state-is-selected)。 –

回答

1

創建一個字符串數組列表,並在文件

頂部初始化並使用ArrayList的在您的微調

adapter1 = 
    new ArrayAdapter<String>(MainActivity.this, 
     android.R.layout.simple_spinner_item, yourarraylist); 

,並添加值到數組列表中你onPostExecute方法

for(int i=0;i<arrSize;i++) { 
      object = prestationlist.getJSONObject(i); 
      value.add(object.getString("Value")); 
      name.add(object.getString("Text")); 
      //Here i've made some display to see if it works, i get the data. 
      yourarraylist.add(object.getString("Text")); 
      responseView.setText(value.toString()); 
     } 
     adapter1.notifyDataSetChanged(); 

注意:您還需要在文件頂部初始化微調器適配器

+0

聽起來是個好主意,但是,當我把加在我的ArrayList它給了我一個錯誤:無法解析方法Add(java.lang.String中) –

+0

好吧是我不好,它的工作! –

+0

我如何通過這樣做來實現我的價值?我想用我的新數組 –