Android > How To Set Json Data With Spinner
im new here, sorry if i doing something idiot question, for the Issues i already create an app like that below, and all the value in the spinner obtained by JSON in this link and t
Solution 1:
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
if(list.get(position).equals("ardie halim")){
listItems2.clear();
listItems2.add("mobile developer");
adapter2.notifyDataSetChanged();
}
else if(list.get(position).equals("indah")){
listItems2.clear();
listItems2.add("database oracle");
adapter2.notifyDataSetChanged();
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Solution 2:
The Below Code is For Set Json Data to Spinner
try {
Gson gson = new Gson();
String json = gson.toJson(response.body());
JSONObject jsonObject = new JSONObject(json);
Log.d("check", "jsonData : " + json);
List<String> allGoverNames = new ArrayList<String>();
allGoverNames.add(0, "Select Governorate");
JSONArray cast = jsonObject.getJSONArray("governorate");
for (int i = 0; i < cast.length(); i++) {
JSONObject actor = cast.getJSONObject(i);
governorateNamenameString = actor.getString("governorate_name");
allGoverNames.add(governorateNamenameString);
}
GoverdataAdapter = new ArrayAdapter<String>
(context, android.R.layout.simple_spinner_item, allGoverNames);
GoverdataAdapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
GovernorateSpinner.setAdapter(GoverdataAdapter);
} catch (JSONException e)
{
e.printStackTrace();
}
Post a Comment for "Android > How To Set Json Data With Spinner"