我的Home Activity中有3个微调器。如果我选择1个其他应该根据第一个微调器改变。我有Country,City和Location作为微调器,如果我选择country,那么city和location应该根据那个改变。到现在为止我设法在spinner中获取国家项目(来自数据库的项目)。现在如何只在spinner中使用选定的项目。我很困惑。 这是我的家庭活动(我在哪里获得微调(国家)项目):
public class Home extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemSelectedListener {
private Spinner countrySpinner, locationSpinner, citySpinner;
private TextView cityCodeTextView;
private Button submitButton;
private ArrayList<String> country_list, location_list, city_list;
private JSONArray result;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
countrySpinner = (Spinner) findViewById(Country);
//citySpinner = (Spinner) findViewById(City);
//locationSpinner = (Spinner) findViewById(R.id.Location);
countrySpinner .setOnItemSelectedListener(this);
country_list = new ArrayList<String>();
//location_list = new ArrayList<String>();
// city_list = new ArrayList<String>();
getData();
}
private void getData(){
StringRequest
stringRequest = new StringRequest(Config.DATA_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
Log.d("Test",response);
JSONArray result = new JSONArray(response);
//Calling method getCountry to get the Country from the JSON Array
getCountry(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}});
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
private void getCountry(JSONArray jsonArrayCountry){
//Traversing through all the items in the json array
List<Country> countries = new ArrayList<>();
try {
String country_name, country_code;
JSONObject countries_object;
for (int i = 0; i < jsonArrayCountry.length(); i++) {
countries_object = jsonArrayCountry.getJSONObject(i);
country_code = countries_object.getString("id");
country_name = countries_object.getString("country");
countries.add(new Country(country_code, country_name));
}
ArrayAdapter countryAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, countries);
countrySpinner.setPrompt("--Select Country--");
countrySpinner.setAdapter(countryAdapter);
countrySpinner.setAdapter(new NothingSelectedSpinnerAdapter(countryAdapter,
R.layout.contact_spinner_row_nothing_selected,this));
countrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
} catch (JSONException e) {
Log.e("Home", e.getLocalizedMessage(), e);
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Setting the values to textviews for a selected item
//textViewName.setText(getName(position));
//textViewCourse.setText(getCourse(position));
//textViewSession.setText(getSession(position));
}
//When no item is selected this method would execute
@Override
public void onNothingSelected(AdapterView<?> parent) {
// textViewName.setText("");
// textViewCourse.setText("");
//textViewSession.setText("");
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}
这是Country.java
public class Country {
private String name;
private String id;
public Country(String id, String name) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
@Override
public String toString() {
return name;
}
}
这是City.java
public class City {
private String name;
public String getName() {
return name;
}
@Override
public String toString() {
return name;
}
}
这是Location.java
public class Location {
private String name;
public String getName() {
return name;
}
@Override
public String toString() {
return name;
}
}
这是Config.java
public class Config {
//JSON URL
public static final String DATA_URL = "http://demo5...../get_country.php";
public static final String DATA_URL1 = "http://demo5...../get_jsoncity.php?id=";
//Tags used in the JSON String
public static final String DATA_URL2 = "http://demo5...../get_jsonlocation.php?id=";
//Tags used in the JSON String
//JSON array name
public static final String JSON_ARRAY = "result";
}
这是我的第一个微调器的json:
[
{
"id": "1",
"country": "UAE"
},
{
"id": "2",
"country": "UK"
},
{
"id": "3",
"country": "SAUDI ARABIA"
},
{
"id": "4",
"country": "OMAN"
},
{
"id": "5",
"country": "BAHRAIN"
},
{
"id": "6",
"country": "INDIA"
}
]
如果我选择id = 1
,这是针对城市的[
{
"id": "1",
"city_name": "Abu Dhabi"
},
{
"id": "2",
"city_name": "Dubai"
},
{
"id": "3",
"city_name": "Sharjah"
},
{
"id": "4",
"city_name": "Ajman"
},
{
"id": "5",
"city_name": "Ummal Qwain"
}
]
我已经获得第一个微调项目,即国家,我需要根据第一个微调器的选择获得城市(第二个微调器)和位置(第三个微调器)的项目。
答案 0 :(得分:0)
您的 country.class 应该具有城市和位置的getter设置器。
public class Country {
private String name;
private String id;
private Location loc;
ArrayList<City> Cityarr = new ArrayList<City>();
public ArrayList<City> getCityarr() {
return Cityarr;
}
public void setCityarr(ArrayList<City> Citya) {
this.Cityarr = Citya;
}
public Country(String id, String name) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public Location getLocation() {
return loc;
}
@Override
public String toString() {
return name;
}
}
和spinner.OnItemSelectedLisenter应该嵌套
country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
vbopr.get(i).getCityarr().size();
cityname.clear();
cityname.add("All");
for (int j = 0; j < vbopr.get(i).getCityarr().size(); j++) {
cityname.add(vbopr.get(i).getCityarr().get(j).getCityname());
}
try {
adpcity.notifyDataSetChanged();
} catch (NullPointerException ne) {
}
adpcity = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_items, cityname);
city.setAdapter(adpcity);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
我的网址包含xml格式的数据 示例
<world>
<country>
<countryid>2</countryid>
<countryname>India</countryname>
<city>
<city_id>1462</city_id>
<city_name>abc</city_name>
</city>
<city>
<city_id>1460</city_id>
<city_name>def</city_name>
</city>
<city>
<city_id>1461</city_id>
<city_name>jkl PLANT</city_name>
</city>
</country>
<country>
<countryid>3</countryid>
<countryname>Australia</countryname>
<city>
<city_id>1462</city_id>
<city_name>gdfg PLANT</city_name>
</city>
<city>
<city_id>1460</city_id>
<city_name>xdfPLANT</city_name>
</city>
<city>
<city_id>1461</city_id>
<city_name>dfgPLANT</city_name>
</city>
<city>
<city_id>617</city_id>
<city_name>dgvdfgg</city_name>
</city>
</country>
</world>
答案 1 :(得分:0)
试试这个:
spinner_1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
if (spinner_1.getSelectedItem().equals("UAE")) {
//cal api url here to get data
// set adapter to spinner_2 here for "UAE" selected
} else if (spinner_1.getSelectedItem().equals("UK")) {
//same..
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
);