第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在Android中制作一個(gè)具有動(dòng)態(tài)下拉菜單的表單

如何在Android中制作一個(gè)具有動(dòng)態(tài)下拉菜單的表單

HUH函數(shù) 2023-09-27 14:20:03
我對(duì) android 很陌生,我正在 android 中制作一個(gè)表單。我希望這個(gè)表格有 2 個(gè)下拉列表。在第一個(gè)下拉列表中,用戶必須選擇州,第二個(gè)下拉列表應(yīng)僅顯示該州的城市,用戶可以從中選擇任何城市/地區(qū)。
查看完整描述

3 回答

?
www說

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊

首先,您需要?jiǎng)?chuàng)建 2 個(gè)不同的模型,如下所示:


創(chuàng)建城市和州數(shù)據(jù)模型如下:


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;

    }


}

現(xiàn)在您需要為 State 創(chuàng)建 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;

    }

}

現(xiàn)在你需要像下面這樣制作 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;

    }

}

現(xiàn)在MainActivity應(yīng)該是這樣的:


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();


    }


}



查看完整回答
反對(duì) 回復(fù) 2023-09-27
?
揚(yáng)帆大魚

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個(gè)贊

選擇您所在國(guó)家/地區(qū)的下拉列表后,例如


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) {


            }

        });


查看完整回答
反對(duì) 回復(fù) 2023-09-27
?
30秒到達(dá)戰(zhàn)場(chǎng)

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊

你在android中提到的下拉菜單我們稱之為spinner。android中有默認(rèn)的spinner,你也可以選擇自定義你的spinner這里是谷歌的官方文檔如何使用它

https://developer.android.com/guide/topics/ui/controls/spinner


查看完整回答
反對(duì) 回復(fù) 2023-09-27
  • 3 回答
  • 0 關(guān)注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)