如何在Android中制作一个具有动态下拉菜单的表单

我对 android 很陌生,我正在 android 中制作一个表单。我希望这个表格有 2 个下拉列表。在第一个下拉列表中,用户必须选择州,第二个下拉列表应仅显示该州的城市,用户可以从中选择任何城市/地区。



HUH函数
浏览 61回答 3
3回答

www说

首先,您需要创建 2 个不同的模型,如下所示:创建城市和州数据模型如下:public class State{&nbsp; &nbsp; private int id;&nbsp; &nbsp; private String name;&nbsp; &nbsp; public State(int id, String name){&nbsp; &nbsp; &nbsp; &nbsp;this.id=id;&nbsp; &nbsp; &nbsp; &nbsp;this.name=name;&nbsp; &nbsp; }&nbsp; &nbsp; public int getId(){&nbsp; &nbsp; &nbsp; &nbsp;return id;&nbsp; &nbsp; }&nbsp; &nbsp; public void setId(int id){&nbsp; &nbsp; &nbsp; &nbsp;this.id= id;&nbsp; &nbsp; }&nbsp; &nbsp; public String getName(){&nbsp; &nbsp; &nbsp; &nbsp;return name;&nbsp; &nbsp; }&nbsp; &nbsp; public void setName(String name){&nbsp; &nbsp; &nbsp; &nbsp;this.name=name;&nbsp; &nbsp; }}public class City{&nbsp; &nbsp; private int id;&nbsp; &nbsp; private String name;&nbsp; &nbsp; private int stateId;&nbsp; &nbsp; public State(int id, String name, int stateId){&nbsp; &nbsp; &nbsp; &nbsp;this.id=id;&nbsp; &nbsp; &nbsp; &nbsp;this.name=name;&nbsp; &nbsp; &nbsp; &nbsp;this.stateId=stateId;&nbsp; &nbsp; }&nbsp; &nbsp; public int getId(){&nbsp; &nbsp; &nbsp; &nbsp;return id;&nbsp; &nbsp; }&nbsp; &nbsp; public void setId(int id){&nbsp; &nbsp; &nbsp; &nbsp;this.id= id;&nbsp; &nbsp; }&nbsp; &nbsp; public String getName(){&nbsp; &nbsp; &nbsp; &nbsp;return name;&nbsp; &nbsp; }&nbsp; &nbsp; public void setName(String name){&nbsp; &nbsp; &nbsp; &nbsp;this.name=name;&nbsp; &nbsp; }&nbsp; &nbsp; public int getStateId(){&nbsp; &nbsp; &nbsp; &nbsp;return stateId;&nbsp; &nbsp; }&nbsp; &nbsp; public void setStateId(int id){&nbsp; &nbsp; &nbsp; &nbsp;this.stateId= id;&nbsp; &nbsp; }}现在您需要为 State 创建 Custom Spinner 适配器:public class StateSpinnerAdapter extends ArrayAdapter<State> {&nbsp; &nbsp; public StateSpinnerAdapter (Context context, ArrayList<State> dataList) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, 0, dataList);&nbsp; &nbsp; }&nbsp; &nbsp; @NonNull&nbsp; &nbsp; @Override&nbsp; &nbsp; public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {&nbsp; &nbsp; &nbsp; &nbsp; return initView(position, convertView, parent);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {&nbsp; &nbsp; &nbsp; &nbsp; return initView(position, convertView, parent);&nbsp; &nbsp; }&nbsp; &nbsp; private View initView(int position, View convertView, ViewGroup parent) {&nbsp; &nbsp; &nbsp; &nbsp; if (convertView == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; convertView = LayoutInflater.from(getContext()).inflate(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; R.layout.spinner_row, parent, false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; TextView textViewName = convertView.findViewById(R.id.tv_name);&nbsp; &nbsp; &nbsp; &nbsp; District currentItem = getItem(position);&nbsp; &nbsp; &nbsp; &nbsp; if (currentItem != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textViewName.setText(currentItem.getName());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return convertView;&nbsp; &nbsp; }}现在你需要像下面这样制作 CitySpinnerAdapter :public class CitySpinnerAdapter extends ArrayAdapter<City> {&nbsp; &nbsp; public CitySpinnerAdapter (Context context, ArrayList<City> dataList) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, 0, dataList);&nbsp; &nbsp; }&nbsp; &nbsp; @NonNull&nbsp; &nbsp; @Override&nbsp; &nbsp; public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {&nbsp; &nbsp; &nbsp; &nbsp; return initView(position, convertView, parent);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {&nbsp; &nbsp; &nbsp; &nbsp; return initView(position, convertView, parent);&nbsp; &nbsp; }&nbsp; &nbsp; private View initView(int position, View convertView, ViewGroup parent) {&nbsp; &nbsp; &nbsp; &nbsp; if (convertView == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; convertView = LayoutInflater.from(getContext()).inflate(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; R.layout.spinner_row, parent, false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; TextView textViewName = convertView.findViewById(R.id.tv_name);&nbsp; &nbsp; &nbsp; &nbsp; Upazila currentItem = getItem(position);&nbsp; &nbsp; &nbsp; &nbsp; if (currentItem != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textViewName.setText(currentItem.getName());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return convertView;&nbsp; &nbsp; }}现在MainActivity应该是这样的:public class MainActivity extends AppCompatActivity{&nbsp; &nbsp; private Context mContext;&nbsp; &nbsp; private Activity mActivity;&nbsp; &nbsp; private ArrayList<State> states;&nbsp; &nbsp; private ArrayList<City> cities;&nbsp; &nbsp; private StateSpinnerAdapter stateAdapter;&nbsp; &nbsp; private CitySpinnerAdapter cityAdapter;&nbsp; &nbsp;private Spinner spState, spCity;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; initVariable();&nbsp; &nbsp; &nbsp; &nbsp; initView();&nbsp; &nbsp; &nbsp; &nbsp; initListener();&nbsp; &nbsp; }&nbsp; &nbsp; private void initVariable() {&nbsp; &nbsp; &nbsp; &nbsp; mContext = getApplicationContext();&nbsp; &nbsp; &nbsp; &nbsp; mActivity = MainActivity.this;&nbsp; &nbsp; &nbsp; &nbsp; states = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; cities = new ArrayList<>();&nbsp; &nbsp; }&nbsp; &nbsp; private void initView() {&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; &nbsp; &nbsp; spState = findViewById(R.id.spState);&nbsp; &nbsp; &nbsp; &nbsp; spCity = findViewById(R.id.cityState);&nbsp; &nbsp; &nbsp; &nbsp; stateAdapter = new StateSpinnerAdapter(mContext,states);&nbsp; &nbsp; &nbsp; &nbsp; spState.setAdapter(stateAdapter);&nbsp; &nbsp; &nbsp; &nbsp; cityAdapter = new CitySpinnerAdapter(mContext,cities);&nbsp; &nbsp; &nbsp; &nbsp; spCity.setAdapter(cityAdapter);&nbsp; &nbsp; &nbsp; &nbsp; loadStateData();&nbsp; &nbsp; }&nbsp; &nbsp; private void initListener(){&nbsp; &nbsp; &nbsp; &nbsp; spState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loadCityData(states.get(i).getId());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onNothingSelected(AdapterView<?> adapterView) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; private void loadStateData(){&nbsp; &nbsp; &nbsp; &nbsp;states.add(new State(1,"state 1"));&nbsp; &nbsp; &nbsp; &nbsp;states.add(new State(2,"state 1"));&nbsp; &nbsp; &nbsp; &nbsp;states.add(new State(3,"state 1"));&nbsp; &nbsp; &nbsp; &nbsp;states.add(new State(4,"state 1"));&nbsp; &nbsp; &nbsp; &nbsp;stateAdapter.notifyDataSetChanged();&nbsp; &nbsp; }&nbsp; &nbsp; private loadCityData(int stateId){&nbsp; &nbsp; &nbsp; &nbsp;ArrayList<City> cityList= new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp;cityList.add(new City(1,"City 1", 1));&nbsp; &nbsp; &nbsp; &nbsp;cityList.add(new City(2,"City 2", 1));&nbsp; &nbsp; &nbsp; &nbsp;cityList.add(new City(3,"City 3", 2));&nbsp; &nbsp; &nbsp; &nbsp;cityList.add(new City(4,"City 4", 2));&nbsp; &nbsp; &nbsp; &nbsp;cityList.add(new City(5,"City 5", 3));&nbsp; &nbsp; &nbsp; &nbsp;cityList.add(new City(6,"City 6", 3));&nbsp; &nbsp; &nbsp; &nbsp;cityList.add(new City(7,"City 7", 4));&nbsp; &nbsp; &nbsp; &nbsp;cityList.add(new City(8,"City 8", 4));&nbsp; &nbsp; &nbsp; &nbsp;cities.addAll(cityList.stream().filter(city -> city.getStateId == stateId).collect(Collectors.toList()));&nbsp; &nbsp; &nbsp; &nbsp;cityAdapter.notifyDataSetChanged();&nbsp; &nbsp; }}

扬帆大鱼

选择您所在国家/地区的下拉列表后,例如spinner_country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i>0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Load your spinner of state and call your data regarding country&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onNothingSelected(AdapterView<?> adapterView) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });

30秒到达战场

你在android中提到的下拉菜单我们称之为spinner。android中有默认的spinner,你也可以选择自定义你的spinner这里是谷歌的官方文档如何使用它https://developer.android.com/guide/topics/ui/controls/spinner
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java