如何根据其他微调器选择过滤微调器?

问题描述 投票:0回答:4

我有3个Spinners citySpinner,regionSpinner,branchSpinner,我可以在spinners中填充数据。问题是如果我从spinnerRegion中选择UP然后在citySpinner我想只显示与该区域相关的城市。请帮助我。

在arraylist中,我现在成功地存储数据如何根据微调器选择来比较数据?

 ArrayList<HashMap<String, String>> formList = new ArrayList<HashMap<String, String>>();
            final HashMap<String, String> m_li;
            m_li = new HashMap<String, String>();
            m_li.put("region", region);
            m_li.put("city", city);
            m_li.put("Branch", branch);
            formList.add(m_li);
            Log.d("DDDD----->", String.valueOf(formList));
android json
4个回答
0
投票

在你的活动中使用它:

    final String JsonData = "[ { \"Br.Name\": \"A BRANCH\", \"Region\": \"UP(W)\", \"City\": \"Agra\" }, { \"Br.Name\": \"B BRANCH\", \"Region\": \"UP(W)\", \"City\": \"Bareilly\" }, { \"Br.Name\": \"C\", \"Region\": \"UP(W)\", \"City\": \"Baghpat\" }, { \"Br.Name\": \"D\", \"Region\": \"UP(E)\", \"City\": \"Amhat\" }, { \"Br.Name\": \"SE\", \"Region\": \"UP(E)\", \"City\": \"Bankeyganj\" }, { \"Br.Name\": \"F\", \"Region\": \"UP Central\", \"City\": \"HARDOI\" }, { \"Br.Name\": \"G\", \"Region\": \"UP(W)\", \"City\": \"Ghaziabad\" } ] ";
    spRegion = findViewById(R.id.spRegion);
    spCity = findViewById(R.id.spCity);


    spRegion.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            ArrayAdapter<String> CityAdapter = new ArrayAdapter<String>(Main2Activity.this, R.layout.support_simple_spinner_dropdown_item, getCity(JsonData, spRegion.getSelectedItem().toString()));
            spCity.setAdapter(CityAdapter);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

并请在此课程代码中替换您所在城市的来源:

 public List<String> getCity(String JsonData, String SelectedRegion) {

    List<String> SpCityList = new ArrayList<>();
    String[] citys;
    try {
        JSONArray Array = new JSONArray(JsonData);

        for (int k = 0; k < Array.length(); k++) {

            try {
                JSONObject object = Array.getJSONObject(k);
                String Region = object.getString("Region");
                String City = object.getString("City");

                if (Region.equals(SelectedRegion)) {
                    SpCityList.add(City);
                }
            } catch (JSONException e) {
                e.printStackTrace();

            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }


    return SpCityList;
}

0
投票

当你解析然后那个时候你必须进行映射对于ex HashMap> regionToCity = new HashMap <>(); regionToCity.put(“UP”,包含Up的城市的arralist); //这必须在解析时完成。

看起来像

regionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String regionName = regiponList.get(position);
            cityList = regionToCity.get(regionName);
            //setAdapter of city spinner
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

希望你明白我在说什么。尝试这个,然后让我知道

这是您理解的一个例子。你想要实现的目标。


0
投票

您必须在解析时根据您的json管理数据

要么

你必须改变你的json响应

{"UP":[
    {
        "Agra": [
                    {"Br.Name": "A BRANCH"},
                    {"Br.Name": "B BRANCH"},
                    {"Br.Name": "C BRANCH"}
                ]
    },
    {
        "Bareilly": 
                [
                    {"Br.Name": "A BRANCH"},
                    {"Br.Name": "B BRANCH"},
                    {"Br.Name": "C BRANCH"}
                ]
    }
],
"MP":[
    {
        "INDORE": [
                    {"Br.Name": "A BRANCH"},
                    {"Br.Name": "B BRANCH"},
                    {"Br.Name": "C BRANCH"}
                ]
    },
    {
        "BHOPAL": 
                [
                    {"Br.Name": "A BRANCH"},
                    {"Br.Name": "B BRANCH"},
                    {"Br.Name": "C BRANCH"}
                ]
    }
]
}

然后你可以用区域到城市列表和城市到分支列表进行映射


0
投票

如果你想根据代码下面的一些其他微调器值显示一个微调器值,例如....

 private void setSpinser() {
    spCity = findViewById(R.id.city);
    spRegion = findViewById(R.id.reigon);
    List<String> cityName = new ArrayList<>();
    final List<String> regionName = new ArrayList<>();
    final ArrayAdapter<String> regionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    cityName.add("Select city");
    cityName.add("Ahmadabad");
    cityName.add("Surat");
    cityName.add("Delhi");
    ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(DbInsert.this, android.R.layout.simple_list_item_1, cityName);
    spCity.setAdapter(stringArrayAdapter);
    spCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            switch (spCity.getSelectedItem().toString())
            {
                case "Ahmadabad":
                    regionName.add("Abc");
                    regionName.add("cde");
                    regionName.add("efg");
                    regionAdapter.addAll(regionName);
                    spRegion.setAdapter(regionAdapter);
                    break;
                case "Surat":
                    regionName.add("pqr");
                    regionName.add("stu");
                    regionName.add("xyz");
                    regionAdapter.addAll(regionName);
                    spRegion.setAdapter(regionAdapter);
                    break;

            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
}

上面的方法根据选中的第一个微调器值设置第二个微调器值,此方法在oncreate方法中调用mainactivity。

© www.soinside.com 2019 - 2024. All rights reserved.