我想在第二個微調的基礎上選擇在第一spinner.if我選擇印度獲得所有國家,但我不想這樣,我只需要印度國家,請幫助me.below粘貼我的代碼基於第二個微調選擇顯示項目
package com.example.sankar.machinetest;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemSelectedListener {
private Spinner countrySpinner, stateSpinner, citySpinner;
private TextView cityCodeTextView;
private Button submitButton;
private ArrayList<String> country_list, state_list, city_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countrySpinner = (Spinner) findViewById(R.id.county);
stateSpinner = (Spinner) findViewById(R.id.states);
citySpinner = (Spinner) findViewById(R.id.city);
country_list = new ArrayList<String>();
state_list = new ArrayList<String>();
city_list = new ArrayList<String>();
try {
JSONObject jsonObject = new JSONObject(loadJsonFromAsset());
JSONArray jsonArray = jsonObject.getJSONArray("countries");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject countries_object = jsonArray.getJSONObject(i);
String country_name = countries_object.getString("name");
String country_code = countries_object.getString("countryCode");
country_list.add(country_name);
JSONArray states_array = countries_object.getJSONArray("states");
for (int j = 0; j < states_array.length(); j++) {
JSONObject states_object = states_array.getJSONObject(j);
String state_name = states_object.getString("name");
state_list.add(state_name);
JSONArray city_array = states_object.getJSONArray("cities");
for (int k = 0; k < city_array.length(); k++) {
JSONObject city_object = city_array.getJSONObject(k);
String city_name = city_object.getString("name");
String city_code = city_object.getString("code");
city_list.add(city_name);
}
}
}
ArrayAdapter<String> country_adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, country_list);
countrySpinner.setAdapter(country_adapter);
ArrayAdapter<String> city_adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, city_list);
citySpinner.setAdapter(city_adapter);
countrySpinner.setOnItemSelectedListener(this);
} catch (JSONException e) {
e.printStackTrace();
}
}
public String loadJsonFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("countries.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return json;
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
if (position==0) {
ArrayAdapter<String> state_adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, state_list);
stateSpinner.setAdapter(state_adapter);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
和我的JSON是
{
"countries": [{
"name": "India",
"countryCode": "91",
"states": [{
"name": "Kerala",
"cities": [{
"name": "Thrissur",
"code": "680111"
}, {
"name": "Kochi",
"code": "680222"
}, {
"name": "Trivandrum",
"code": "680333"
}]
}, {
"name": "TamilNadu",
"cities": [{
"name": "Chennai",
"code": "380111"
}, {
"name": "Coimbatore",
"code": "380222"
}]
}]
}, {
"name": "Germany",
"countryCode": "49",
"states": [{
"name": "Bayern",
"cities": [{
"name": "Nurnburg",
"code": "123"
}, {
"name": "Munich",
"code": "125"
}]
}, {
"name": "Berlin",
"cities": [{
"name": "Berlin",
"code": "223"
}]
}]
}]
}
你可以發佈您的適配器的代碼? –
謝謝,適配器代碼只包含在上面的代碼中。 –
我需要國家根據國家選擇和基於我想要的城市狀態,請幫我 –