我正在使用我自己製作的一些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裏面?
非常感謝!
請參閱[這個答案](https://stackoverflow.com/questions/7370107/update-city-spinner-with-notifydatasetchanged-after-state-is-selected)。 –