3 回答
TA贡献1775条经验 获得超8个赞
首先,您需要创建 2 个不同的模型,如下所示:
创建城市和州数据模型如下:
public class State{
private int id;
private String name;
public State(int id, String name){
this.id=id;
this.name=name;
}
public int getId(){
return id;
}
public void setId(int id){
this.id= id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
}
public class City{
private int id;
private String name;
private int stateId;
public State(int id, String name, int stateId){
this.id=id;
this.name=name;
this.stateId=stateId;
}
public int getId(){
return id;
}
public void setId(int id){
this.id= id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getStateId(){
return stateId;
}
public void setStateId(int id){
this.stateId= id;
}
}
现在您需要为 State 创建 Custom Spinner 适配器:
public class StateSpinnerAdapter extends ArrayAdapter<State> {
public StateSpinnerAdapter (Context context, ArrayList<State> dataList) {
super(context, 0, dataList);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return initView(position, convertView, parent);
}
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return initView(position, convertView, parent);
}
private View initView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.spinner_row, parent, false
);
}
TextView textViewName = convertView.findViewById(R.id.tv_name);
District currentItem = getItem(position);
if (currentItem != null) {
textViewName.setText(currentItem.getName());
}
return convertView;
}
}
现在你需要像下面这样制作 CitySpinnerAdapter :
public class CitySpinnerAdapter extends ArrayAdapter<City> {
public CitySpinnerAdapter (Context context, ArrayList<City> dataList) {
super(context, 0, dataList);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return initView(position, convertView, parent);
}
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return initView(position, convertView, parent);
}
private View initView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.spinner_row, parent, false
);
}
TextView textViewName = convertView.findViewById(R.id.tv_name);
Upazila currentItem = getItem(position);
if (currentItem != null) {
textViewName.setText(currentItem.getName());
}
return convertView;
}
}
现在MainActivity应该是这样的:
public class MainActivity extends AppCompatActivity{
private Context mContext;
private Activity mActivity;
private ArrayList<State> states;
private ArrayList<City> cities;
private StateSpinnerAdapter stateAdapter;
private CitySpinnerAdapter cityAdapter;
private Spinner spState, spCity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initVariable();
initView();
initListener();
}
private void initVariable() {
mContext = getApplicationContext();
mActivity = MainActivity.this;
states = new ArrayList<>();
cities = new ArrayList<>();
}
private void initView() {
setContentView(R.layout.activity_main);
spState = findViewById(R.id.spState);
spCity = findViewById(R.id.cityState);
stateAdapter = new StateSpinnerAdapter(mContext,states);
spState.setAdapter(stateAdapter);
cityAdapter = new CitySpinnerAdapter(mContext,cities);
spCity.setAdapter(cityAdapter);
loadStateData();
}
private void initListener(){
spState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
loadCityData(states.get(i).getId());
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
private void loadStateData(){
states.add(new State(1,"state 1"));
states.add(new State(2,"state 1"));
states.add(new State(3,"state 1"));
states.add(new State(4,"state 1"));
stateAdapter.notifyDataSetChanged();
}
private loadCityData(int stateId){
ArrayList<City> cityList= new ArrayList<>();
cityList.add(new City(1,"City 1", 1));
cityList.add(new City(2,"City 2", 1));
cityList.add(new City(3,"City 3", 2));
cityList.add(new City(4,"City 4", 2));
cityList.add(new City(5,"City 5", 3));
cityList.add(new City(6,"City 6", 3));
cityList.add(new City(7,"City 7", 4));
cityList.add(new City(8,"City 8", 4));
cities.addAll(cityList.stream().filter(city -> city.getStateId == stateId).collect(Collectors.toList()));
cityAdapter.notifyDataSetChanged();
}
}
TA贡献1799条经验 获得超9个赞
选择您所在国家/地区的下拉列表后,例如
spinner_country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(i>0)
{
//Load your spinner of state and call your data regarding country
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
TA贡献1828条经验 获得超6个赞
你在android中提到的下拉菜单我们称之为spinner。android中有默认的spinner,你也可以选择自定义你的spinner这里是谷歌的官方文档如何使用它
https://developer.android.com/guide/topics/ui/controls/spinner
添加回答
举报