如何通过更改微调器1值来清除微调器2的值

时间:2018-04-07 10:46:29

标签: android http spinner

在我的应用程序中,我有两个微调器,第一个微调器包含一些值,如(str1,str2,str3 ...),我的第二个微调器包含一些与第一个微调器的每个值相关的值。当我从第一个微调器中选择一个值时,相关的值将在第二个微调器中为此完成的代码完美地执行,如下所示。

这是我的第一个微调代码:

 private HashMap<String,String>departmentMap=new HashMap<>();
private void loadSpinnerData(String url) {

    try {

        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpPost = new HttpGet(url);
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();

        String responseString = EntityUtils.toString(entity);

        JSONArray jsonarray = new JSONArray(responseString);
        for (int i = 0; i < jsonarray.length(); i++) {
            JSONObject jsonobject = jsonarray.getJSONObject(i);
            String department = jsonobject.getString("Department");
            uid = jsonobject.getString("ID");
            Department.add(department);
            departmentMap.put(department,uid);
        }

        spinner.setAdapter(new ArrayAdapter<String>(NewDeficiency.this, android.R.layout.simple_spinner_dropdown_item, Department));

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

这属于第一个微调器项目选择:

 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l){
            String department=spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString();


Toast.makeText(getApplicationContext(),department,Toast.LENGTH_LONG).show();
        String id=departmentMap.get(department);
        loadSpinnerDeficiency(URL2,id);
    }
    @Override
    public void onNothingSelected(AdapterView<?> adapterView){
        // DO Nothing here
    }
});

以下是从spinner2得到适当的回应:

 private void loadSpinnerDeficiency(String url2,String uid) {

        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpPost = new HttpGet("http://182.18.163.39/train/m/def.php?issue=2&dept="+uid);
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();

            String responseString = EntityUtils.toString(entity);

            JSONArray jsonarray = new JSONArray(responseString);
            for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject jsonobject = jsonarray.getJSONObject(i);
                String defcat = jsonobject.getString("Deficiency");
                defid = jsonobject.getString("ID");

                Deficiency.add(defcat);
            }

            spinner2.setAdapter(new ArrayAdapter<String>(NewDeficiency.this, android.R.layout.simple_spinner_dropdown_item, Deficiency));

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

这里在旋转器1中更改我的第一个选择时,必须清除spinner2选择的第一个选择值。 但它没有发生,请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

在代码下面使用..

/**
 * remove second spinner value based on first spinner selected value
 * it match then remove it.
 */
private void removeValue() {
    List<String> itemvalue=new ArrayList<>();
    itemvalue.add("Abc");
    itemvalue.add("cde");
    itemvalue.add("xyz");
    List<String> itemvalue2=new ArrayList<>();
    itemvalue2.add("Abc");
    itemvalue2.add("cde");
    itemvalue2.add("xyz");
    itemvalue2.add("pqr");
    itemvalue2.add("stu");
    Spinner spinner=findViewById(R.id.sp);
    Spinner spinner1=findViewById(R.id.sptwo);
    ArrayAdapter<String> adapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,itemvalue);
    ArrayAdapter<String> adapter2=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,itemvalue2);
    spinner.setAdapter(adapter);
    spinner1.setAdapter(adapter2);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            itemvalue2.remove(spinner.getSelectedItem());
            adapter2.notifyDataSetChanged();
        }

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

        }
    });

}