gpt4 book ai didi

java - 如何在Android中选择上一个微调器时获取微调器项目?

转载 作者:行者123 更新时间:2023-11-29 19:30:06 27 4
gpt4 key购买 nike

我的家庭 Activity 中有 3 个微调器。如果我选​​择其他 1 个微调器,则应根据第一个微调器进行更改。我将国家/地区、城市和位置作为微调器,如果我选择国家/地区,则城市和位置应据此更改。到目前为止我设法在微调器中获取国家/地区项目(来自数据库的项目)。现在如何在微调器中仅获取选定的项目..我很困惑。?这是我的家庭 Activity (我在其中获得旋转器(国家/地区)项目):

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.class应该具有城市和位置的getter setter。

    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>

关于java - 如何在Android中选择上一个微调器时获取微调器项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41843966/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com