如何在Android Studio中加载与州选择器相对应的城市选择器。

huangapple 未分类评论43阅读模式
英文:

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.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.StateCity&quot;&gt;

    &lt;LinearLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginLeft=&quot;@dimen/margin_45&quot;
        android:layout_marginRight=&quot;@dimen/margin_25&quot;
        android:orientation=&quot;vertical&quot;&gt;


        &lt;TextView
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:text=&quot;state&quot;
            android:layout_marginTop=&quot;20dp&quot;/&gt;

        &lt;RelativeLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:background=&quot;@drawable/spinner&quot;
            android:orientation=&quot;horizontal&quot;
            android:layout_marginTop=&quot;0dp&quot;&gt;

            &lt;Spinner
                android:id=&quot;@+id/spinnerState&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;@dimen/height_50&quot;
                android:layout_centerVertical=&quot;true&quot;
                android:layout_gravity=&quot;center&quot;
                android:background=&quot;@android:color/transparent&quot;
                android:gravity=&quot;center&quot;
                android:spinnerMode=&quot;dropdown&quot; /&gt;

            &lt;ImageView
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_alignParentRight=&quot;true&quot;
                android:layout_centerVertical=&quot;true&quot;
                android:layout_gravity=&quot;center&quot;
                android:src=&quot;@drawable/ic_expand_more&quot; /&gt;

        &lt;/RelativeLayout&gt;


        &lt;TextView
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:text=&quot;city&quot;
            android:layout_marginTop=&quot;20dp&quot;/&gt;
        &lt;RelativeLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:background=&quot;@drawable/spinner&quot;
            android:orientation=&quot;horizontal&quot;
            android:layout_marginTop=&quot;0dp&quot;&gt;

            &lt;Spinner
                android:id=&quot;@+id/spinnerCity&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;@dimen/height_50&quot;
                android:layout_centerVertical=&quot;true&quot;
                android:layout_gravity=&quot;center&quot;
                android:background=&quot;@android:color/transparent&quot;
                android:gravity=&quot;center&quot;
                android:spinnerMode=&quot;dropdown&quot; /&gt;

            &lt;ImageView
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_alignParentRight=&quot;true&quot;
                android:layout_centerVertical=&quot;true&quot;
                android:layout_gravity=&quot;center&quot;
                android:src=&quot;@drawable/ic_expand_more&quot; /&gt;
        &lt;/RelativeLayout&gt;
    &lt;/LinearLayout&gt;
&lt;/RelativeLayout&gt;
    public class StateCity extends BaseActivity {
    
        Spinner spinnerState, spinnerCity;
    
        ArrayList&lt;GetState&gt; stateArray = new ArrayList&lt;&gt;();
        ArrayList&lt;CityListModel&gt; cityArray = new ArrayList&lt;&gt;();
    
        ArrayAdapter&lt;CityListModel&gt; arrayAdapterCT;
    
        int state_id;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_state_city);
    
    
            stateArray = new ArrayList&lt;&gt;();
            cityArray = new ArrayList&lt;&gt;();
    
    
            spinnerState = findViewById(R.id.spinnerState);
            spinnerCity = findViewById(R.id.spinnerCity);
    
            getLocalState();
            //getLocalCity();
    
    
        }
    
        @Override
        protected String getTag() {
            return &quot;StateCity&quot;;
        }
        private void getLocalState() {
            long count = LocalStateModel.count(LocalStateModel.class);
            if (count &gt; 0) {
                stateArray.add(new GetState(0, &quot;Select State&quot;));
                List&lt;LocalStateModel&gt; localStateModels = new LocalStateModel().listAll(LocalStateModel.class);
                for (int i = 0; i &lt; localStateModels.size(); i++) {
                    stateArray.add(new GetState(localStateModels.get(i).getState_id(), localStateModels.get(i).getName()));
                }
                setSpinner();
            }
        }
        @SuppressLint(&quot;NewApi&quot;)
        private void getLocalCity() {
            long count = LocalCityModel.count(LocalCityModel.class);
            if (count &gt; 0) {
                //cityArray.add(new CityListModel(0, &quot;Select City&quot;));
                List&lt;LocalCityModel&gt; localCityModels = new LocalCityModel().listAll(LocalCityModel.class);
                cityArray.add(new CityListModel(0, &quot;Select City&quot;));
    
                for (int i = 0; i &lt; localCityModels.size(); i++) {
                    cityArray.add(new CityListModel(localCityModels.get(i).getCity_id(), localCityModels.get(i).getCity_name()));
                }
                setCitySpinner();
            }
    
        }
    
    
    
        public void setSpinner(){
            final ArrayAdapter&lt;GetState&gt; arrayAdapter14 = new ArrayAdapter&lt;GetState&gt;(
                    this, android.R.layout.simple_spinner_item, stateArray) {
                @Override
                public boolean isEnabled(int position) {
                    if (position == 0) {
    
                        return false;
                    } else {
                        return true;
                    }
                }
    
                @SuppressLint(&quot;ResourceAsColor&quot;)
                @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&lt;?&gt; parent, View view, int position, long id) {
                    if (position &gt; 0) {
                        GetState getType = (GetState) parent.getSelectedItem();
                        /*ArrayList&lt;CityListModel&gt; tempCity = new ArrayList&lt;&gt;();
                        for (CityListModel getState : cityArray) {
                            if (getState.getState_id() == getType.getState_id()) {
                                tempCity.add(getState);
                            }
    
                            arrayAdapterCT = new ArrayAdapter&lt;CityListModel&gt;(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&lt;?&gt; parent) {
    
                }
            });
    
    
        }
    
        public void setCitySpinner(){
            final ArrayAdapter&lt;CityListModel&gt; arrayAdapter17 = new ArrayAdapter&lt;CityListModel&gt;(
                    this, android.R.layout.simple_spinner_item, cityArray) {
                @Override
                public boolean isEnabled(int position) {
                    if (position == 0) {
    
                        return false;
                    } else {
                        return true;
                    }
                }
    
                @SuppressLint(&quot;ResourceAsColor&quot;)
                @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&lt;?&gt; parent, View view, int position, long id) {
                    CityListModel getType = (CityListModel) parent.getSelectedItem();
                }
    
                @Override
                public void onNothingSelected(AdapterView&lt;?&gt; 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 &amp;&amp; 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&lt;GetCity&gt; 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&lt;GetCity&gt; getCity() {
            return city;
        }
    
        public void setCity(List&lt;GetCity&gt; 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 &amp;&amp; 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 = {&quot;state 1&quot;,&quot;state 2&quot;,&quot;state 3&quot;};
    String[] car = {&quot;car 1&quot;,&quot;car 2&quot;,&quot;car 3&quot;};
    String[] category = {&quot;state&quot;,&quot;car&quot;};

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&lt;?&gt; parent, View view, int position, long id) {
                String Value = category[position];
                Toast.makeText(MainActivity.this, Value, Toast.LENGTH_SHORT).show();

                if (Value.contentEquals(&quot;state&quot;)){
                    ArrayAdapter adapter1 = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,state);
                    sp2.setAdapter(adapter1);
                }else if (Value.contentEquals(&quot;car&quot;)){
                    ArrayAdapter adapter1 = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,car);
                    sp2.setAdapter(adapter1);
                }
            }

            @Override
            public void onNothingSelected(AdapterView&lt;?&gt; parent) {

            }
        });

Hopes this is what you asked for

</details>



huangapple
  • 本文由 发表于 2020年4月4日 17:13:39
  • 转载请务必保留本文链接:https://java.coder-hub.com/61025991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定