簡體   English   中英

如何在Android中選擇上一個微調器時獲得微調器項目?

[英]how to get spinner item on selection of previous spinner in android?

我的家庭活動中有3個微調器。如果我選​​擇1個應根據第一個微調器更改。我有國家/地區,城市和位置作為微調器,如果我選擇國家/地區,則城市和位置應根據此更改。在spinner中獲取國家項目(從數據庫中獲取項目)。現在如何僅在spinner..im中獲取所選項目感到困惑。 這是我的家庭活動(我在其中獲得微調(國家)項目):

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"
  }
]

我已經有了第一個微調器項目,即Country,我需要根據第一個微調器的選擇獲取城市(第二個微調器)和位置(第三個微調器)的項目。

您的country.class應該具有城市和位置的獲取方法。

    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>

嘗試這個:

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) {
                }
            }
    );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM