我想在我的微調器上設置onItemSelectedListener,所以如果從微調器中選擇一個值,它將顯示選定的值。我的代碼,微調框上的setOnItemSelectedListener?
ArrayList<String> items = getCountries("data.json");
spinner = (Spinner)findViewById(R.id.spinnerStandard);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.second_layout,R.id.txtStandard,items);
spinner.setAdapter(adapter);
}
private ArrayList<String> getCountries(String fileName){
JSONArray jsonArray = null;
ArrayList<String> cList = new ArrayList<String>();
try {
InputStream is = getResources().getAssets().open(fileName);
int size = is.available();
byte[] data = new byte[size];
is.read(data);
is.close();
String json = new String(data, "UTF-8");
jsonArray=new JSONArray(json);
if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
String standard = jsonArray.getJSONObject(i).getString("standard");
if(!cList.contains(standard))
cList.add(standard);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
catch (JSONException je)
{
je.printStackTrace();
}
return cList;
}
我試着用搜索引擎的所有選項,因爲我能想到的,並不能找到任何有用的結果爲許多其他的變化,所以我顯然失去了一些東西。
做這種情況的正確方法是什麼?
是的,你是正確的。使用'onItemSelectedListener'你可以實現你的任務。 –