2017-01-22 68 views
0

我想在第二個微調的基礎上選擇在第一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" 
     }] 
    }] 
    }] 
} 
+0

你可以發佈您的適配器的代碼? –

+0

謝謝,適配器代碼只包含在上面的代碼中。 –

+0

我需要國家根據國家選擇和基於我想要的城市狀態,請幫我 –

回答

0
String countryCode = null; 

You can call your second spinner in onItemSelected method. 

countryCode = countrySpinner.getSelectedItem().toString(); 

// call your second adapter 

before populate state list 

HashMap<String, String> hmap = new HashMap<String, String>(); 
hmap.put(country_code, stateName); 
+0

謝謝,但我可以在哪裏通過這coutryCode。 –

+0

填充狀態時,您沒有添加任何國家/地區代碼。 –

+0

HashMap hmap = new HashMap (); –

1

你可以試試這個

public class Country { 

    private String name; 
    private String code; 
    private List<State> states; 

    public Country(String name, String code, List<State> states) { 
     this.name = name; 
     this.code = code; 
     this.states = states; 
    } 

    public String getName() { 
     return name; 
    } 

    public String getCode() { 
     return code; 
    } 

    public List<State> getStates() { 
     return states; 
    } 

    @Override 
    public String toString() { 
     return name; 
    } 
} 

public class State { 

    private String name; 
    private List<City> cities; 

    public State(String name, List<City> cities) { 
     this.name = name; 
     this.cities = cities; 
    } 

    public String getName() { 
     return name; 
    } 

    public List<City> getCities() { 
     return cities; 
    } 

    @Override 
    public String toString() { 
     return name; 
    } 
} 

public class City { 

    private String name; 
    private String code; 

    public City(String name, String code) { 
     this.name = name; 
     this.code = code; 
    } 

    public String getName() { 
     return name; 
    } 

    public String getCode() { 
     return code; 
    } 

    @Override 
    public String toString() { 
     return name; 
    } 
} 

然後從JSON建立你的數據是這樣

try { 
     JSONObject jsonObject = new JSONObject(loadJsonFromAsset()); 
     JSONArray jsonArray = jsonObject.getJSONArray("countries"); 

     List<Country> countries; 
     List<City> cities; 
     List<State> states; 

     countries = new ArrayList<>(); 
     for (int i = 0; i < jsonArray.length(); i++) { 
      JSONObject countries_object = jsonArray.getJSONObject(i); 
      // get country name and code 
      String country_name = countries_object.getString("name"); 
      String country_code = countries_object.getString("countryCode"); 

      // get country states 
      JSONArray states_array = countries_object.getJSONArray("states"); 
      states = new ArrayList<>(); 

      for (int j = 0; j < states_array.length(); j++) { 
       JSONObject states_object = states_array.getJSONObject(j); 
       // get state name 
       String state_name = states_object.getString("name"); 
       // get all city for this state 
       JSONArray city_array = states_object.getJSONArray("cities"); 
       cities = new ArrayList<>(); 
       for (int k = 0; k < city_array.length(); k++) { 
        JSONObject city_object = city_array.getJSONObject(k); 
        // get city name and code 
        String city_name = city_object.getString("name"); 
        String city_code = city_object.getString("code"); 
        // add new city 
        cities.add(new City(city_name, city_code)); 
       } 
       // add new state with cities 
       states.add(new State(state_name, cities)); 
      } 
      countries.add(new Country(country_name, country_code, states)); 
     } 

     ArrayAdapter<Country> country_adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, countries); 
     country_adapter.setAdapter(mCountryAdapter); 
     country_adapter.setOnItemSelectedListener(this); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

最後

@Override 
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
    Country country = (Country) mCountrySpinner.getSelectedItem(); 
    mStateSpinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, country.getStates())); 
} 
+0

哪裏是他在代碼中使用url –

+0

這個類的名稱,其中你指定國家,州和城市 –

+0

嗨@ android_hub ..你可以告訴我在哪裏定義這個公共類國家,城市和州... n在哪類..kindly幫助 –

相關問題