私はスピナーで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'を使って、あなたは仕事を達成することができます。 –