gpt4 book ai didi

java - 如何在具有动态下拉列表的Android中制作表单

转载 作者:行者123 更新时间:2023-11-30 05:00:58 24 4
gpt4 key购买 nike

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

最佳答案

首先,您需要创建 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();

}

}

关于java - 如何在具有动态下拉列表的Android中制作表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58245551/

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