我想在我的微调器上设置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'你可以实现你的任务。 –