我从 JSON 响应中获得了“client_id”和“组织”列表,需要将这些组织列表传递给 spinner,但是当用户从 spinner 中选择组织名称时,需要将与该组织关联的 client_id 传递给服务器。
这是我的代码。
try {
//*** Getting Array of Attributes
attributes = jsonreturn.getJSONObject(TAG_ATTRIBUTE);
String status = attributes.getString(TAG_VALIDCODE);
JSONObject clients = jsonreturn.getJSONObject(TAG_CLIENTS);
JSONArray client = clients.getJSONArray(TAG_CLIENT);
final String[] list_client_id = new String[client.length()+1];
list_client_id[0]= "Select Client";
final String[] list_client_name = new String[client.length()];
if(status.equals("200")){
for(int i = 0; i < client.length(); i++)
{
JSONObject clientlist = client.getJSONObject(i);
//***** Storing each JSON item in variable
String client_id = clientlist.getString(TAG_CLIENT_ID);
String organization = clientlist.getString(TAG_ORGANIZATION);
list_client_id[i+1]= clientlist.getString(TAG_CLIENT_ID);
list_client_name[i]= clientlist.getString(TAG_ORGANIZATION);
}
}
else{
Toast.makeText(this, "Invalid Details", 1000).show(); }
ClientNameSpinner = (Spinner)findViewById(R.id.ClientId);
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, list_client_name);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ClientNameSpinner.setAdapter(adapter);
/* ArrayAdapter<String> adapterid = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, list_client_id);
adapterid.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ClientNameSpinner.setAdapter(adapterid);*/
}
catch (JSONException e)
{
e.printStackTrace();
}