英文:
how to load the city spinner against state spinner in android studio
问题
我想将城市选择器设置为州选择器的对应项。我已经创建了两个选择器,一个用于州,另一个用于城市。当我在州选择器中选择一个州时,我希望在城市选择器中获取该州的所有城市列表。你能帮助我解决这个问题吗?以下是我的代码。
public class StateCity extends BaseActivity {
Spinner spinnerState, spinnerCity;
ArrayList<GetState> stateArray = new ArrayList<>();
ArrayList<CityListModel> cityArray = new ArrayList<>();
ArrayAdapter<CityListModel> arrayAdapterCT;
int state_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_state_city);
stateArray = new ArrayList<>();
cityArray = new ArrayList<>();
spinnerState = findViewById(R.id.spinnerState);
spinnerCity = findViewById(R.id.spinnerCity);
getLocalState();
//getLocalCity();
}
@Override
protected String getTag() {
return "StateCity";
}
private void getLocalState() {
// 从本地获取州数据
// ...
setSpinner();
}
private void getLocalCity() {
// 从本地获取城市数据
// ...
setCitySpinner();
}
public void setSpinner() {
// 设置州选择器的适配器和监听器
// ...
}
public void setCitySpinner() {
// 设置城市选择器的适配器和监听器
// ...
}
}
public class CityListModel implements Serializable {
// 城市模型类
// ...
}
public class GetState implements Serializable {
// 州模型类
// ...
}
注意:上述代码片段中的部分内容被省略,只保留了与你的问题相关的部分。完整的代码需要包括省略部分以及其他可能需要的代码来完成数据获取和设置适配器的过程。
英文:
I want to set the city spinner against the state spinner. I have taken 2 spinner one for state and other for city. When i select the state in state spinner i want to get the all the list of city in city spinner.Can u help me to solve this.Here is my code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StateCity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_45"
android:layout_marginRight="@dimen/margin_25"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="state"
android:layout_marginTop="20dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/spinner"
android:orientation="horizontal"
android:layout_marginTop="0dp">
<Spinner
android:id="@+id/spinnerState"
android:layout_width="match_parent"
android:layout_height="@dimen/height_50"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:gravity="center"
android:spinnerMode="dropdown" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:src="@drawable/ic_expand_more" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="city"
android:layout_marginTop="20dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/spinner"
android:orientation="horizontal"
android:layout_marginTop="0dp">
<Spinner
android:id="@+id/spinnerCity"
android:layout_width="match_parent"
android:layout_height="@dimen/height_50"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:gravity="center"
android:spinnerMode="dropdown" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:src="@drawable/ic_expand_more" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
public class StateCity extends BaseActivity {
Spinner spinnerState, spinnerCity;
ArrayList<GetState> stateArray = new ArrayList<>();
ArrayList<CityListModel> cityArray = new ArrayList<>();
ArrayAdapter<CityListModel> arrayAdapterCT;
int state_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_state_city);
stateArray = new ArrayList<>();
cityArray = new ArrayList<>();
spinnerState = findViewById(R.id.spinnerState);
spinnerCity = findViewById(R.id.spinnerCity);
getLocalState();
//getLocalCity();
}
@Override
protected String getTag() {
return "StateCity";
}
private void getLocalState() {
long count = LocalStateModel.count(LocalStateModel.class);
if (count > 0) {
stateArray.add(new GetState(0, "Select State"));
List<LocalStateModel> localStateModels = new LocalStateModel().listAll(LocalStateModel.class);
for (int i = 0; i < localStateModels.size(); i++) {
stateArray.add(new GetState(localStateModels.get(i).getState_id(), localStateModels.get(i).getName()));
}
setSpinner();
}
}
@SuppressLint("NewApi")
private void getLocalCity() {
long count = LocalCityModel.count(LocalCityModel.class);
if (count > 0) {
//cityArray.add(new CityListModel(0, "Select City"));
List<LocalCityModel> localCityModels = new LocalCityModel().listAll(LocalCityModel.class);
cityArray.add(new CityListModel(0, "Select City"));
for (int i = 0; i < localCityModels.size(); i++) {
cityArray.add(new CityListModel(localCityModels.get(i).getCity_id(), localCityModels.get(i).getCity_name()));
}
setCitySpinner();
}
}
public void setSpinner(){
final ArrayAdapter<GetState> arrayAdapter14 = new ArrayAdapter<GetState>(
this, android.R.layout.simple_spinner_item, stateArray) {
@Override
public boolean isEnabled(int position) {
if (position == 0) {
return false;
} else {
return true;
}
}
@SuppressLint("ResourceAsColor")
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
TextView tv = (TextView) view;
if (position == 0) {
tv.setTextColor(Color.GRAY);
tv.setBackgroundColor(Color.WHITE);
} else {
tv.setTextColor(Color.BLACK);
}
return view;
}
};
arrayAdapter14.setDropDownViewResource(R.layout.spinner_item);
spinnerState.setAdapter(arrayAdapter14);
spinnerState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position > 0) {
GetState getType = (GetState) parent.getSelectedItem();
/*ArrayList<CityListModel> tempCity = new ArrayList<>();
for (CityListModel getState : cityArray) {
if (getState.getState_id() == getType.getState_id()) {
tempCity.add(getState);
}
arrayAdapterCT = new ArrayAdapter<CityListModel>(StateCity.this, R.layout.spinner_item, tempCity);
arrayAdapterCT.setDropDownViewResource(R.layout.spinner_item);
spinnerCity.setAdapter(arrayAdapterCT);
}*/
/*MyApplication.getInstance().addIntToSharedPreference(STATE_TYPE1, getType.getState_id());
state_id = getType.getState_id();*/
//getLocalCity();
//setCitySpinner();
//setCitySpinner();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void setCitySpinner(){
final ArrayAdapter<CityListModel> arrayAdapter17 = new ArrayAdapter<CityListModel>(
this, android.R.layout.simple_spinner_item, cityArray) {
@Override
public boolean isEnabled(int position) {
if (position == 0) {
return false;
} else {
return true;
}
}
@SuppressLint("ResourceAsColor")
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
TextView tv = (TextView) view;
if (position == 0) {
tv.setTextColor(Color.GRAY);
tv.setBackgroundColor(Color.WHITE);
} else {
tv.setTextColor(Color.BLACK);
}
return view;
}
};
arrayAdapter17.setDropDownViewResource(R.layout.spinner_item);
spinnerCity.setAdapter(arrayAdapter17);
spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
CityListModel getType = (CityListModel) parent.getSelectedItem();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
enter code here
public class CityListModel implements Serializable {
int id, state_id;
String city_name;
public CityListModel(int id, String city_name) {
this.id = id;
this.city_name = city_name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getState_id() {
return state_id;
}
public void setState_id(int state_id) {
this.state_id = state_id;
}
public String getCity_name() {
return city_name;
}
public void setCity_name(String city_name) {
this.city_name = city_name;
}
@Override
public String toString() {
return city_name;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof CityListModel){
CityListModel c = (CityListModel) obj;
if(c.getId() == id && c.getCity_name().equals(city_name) )
return true;
}
return false;
}
}
enter code here
public class GetState implements Serializable {
int state_id ;
String name, user_id;
List<GetCity> city;
public GetState(int state_id, String name) {
this.state_id = state_id;
this.name = name;
}
public GetState() {
}
public int getState_id() {
return state_id;
}
public void setState_id(int state_id) {
this.state_id = state_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public List<GetCity> getCity() {
return city;
}
public void setCity(List<GetCity> city) {
this.city = city;
}
@Override
public String toString() {
return name;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof GetState){
GetState c = (GetState) obj;
if(c.getState_id() == state_id && c.getName().equals(name) )
return true;
}
return false;
}
}
</details>
# 答案1
**得分**: 0
我只会给你一个关于如何实现这个的提示
String[] state = {"状态 1", "状态 2", "状态 3"};
String[] car = {"汽车 1", "汽车 2", "汽车 3"};
String[] category = {"状态", "汽车"};
而且这就是它的工作原理
final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, category);
sp1.setAdapter(adapter);
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String Value = category[position];
Toast.makeText(MainActivity.this, Value, Toast.LENGTH_SHORT).show();
if (Value.contentEquals("状态")) {
ArrayAdapter adapter1 = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, state);
sp2.setAdapter(adapter1);
} else if (Value.contentEquals("汽车")) {
ArrayAdapter adapter1 = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, car);
sp2.setAdapter(adapter1);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
希望这是你所要求的。
<details>
<summary>英文:</summary>
I will just give you an hint how to do that
String[] state = {"state 1","state 2","state 3"};
String[] car = {"car 1","car 2","car 3"};
String[] category = {"state","car"};
And this is how it works
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,category);
sp1.setAdapter(adapter);
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String Value = category[position];
Toast.makeText(MainActivity.this, Value, Toast.LENGTH_SHORT).show();
if (Value.contentEquals("state")){
ArrayAdapter adapter1 = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,state);
sp2.setAdapter(adapter1);
}else if (Value.contentEquals("car")){
ArrayAdapter adapter1 = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,car);
sp2.setAdapter(adapter1);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Hopes this is what you asked for
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论