使用Android中的First Spinner填充Spinner

时间:2016-01-08 04:21:04

标签: android json android-spinner android-json

朋友!!我有两个Spinner,Data来自JSON。我能够PARSE数据并显示到Spinner中。在第一个Spinner中有多个节点,其中包含详细信息的编号。所以我的要求是根据第一个微调器的选择填充第二个微调器..请帮助我

@Override
    protected JSONObject doInBackground(String... args) {

        JSONParser jParser = new JSONParser();

        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);
        return json;
    }
     @Override
     protected void onPostExecute(JSONObject json) {
         pDialog.dismiss();
         try {
                // Getting JSON Array from URL
                android = json.getJSONArray(TAG_OS);
                for(int i = 0; i < android.length(); i++){
                JSONObject c = android.getJSONObject(i);
                c = c.getJSONObject("LearningStandards");

                // Storing  JSON item in a Variable
                String ver = c.getString(TAG_VER);
                /*String name = c.getString(TAG_NAME);
                String api = c.getString(TAG_API);*/




                // Adding value HashMap key => value


                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_VER, ver);
                /*map.put(TAG_NAME, name);
                map.put(TAG_API, api);*/

                oslist.add(map);
                list=(ListView)findViewById(R.id.list);





                ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
                        R.layout.list_v,
                        new String[] { TAG_VER,TAG_NAME, TAG_API }, new int[] {
                                R.id.vers,R.id.name, R.id.api});

                list.setAdapter(adapter);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Toast.makeText(MainActivity.this, "You Clicked at "+oslist.get(+position).get("ref_id"), Toast.LENGTH_SHORT).show();

                    }
                });

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

1 个答案:

答案 0 :(得分:0)

使用: spinner.setOnItemSelectedListener

我创建了Category和Subcategory微调器。子类别值基于类别选择显示。 以下是代码示例:

    spn_category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                    String selection = spn_category.getSelectedItem().toString();
                ArrayAdapter<String> selection_subcat = new ArrayAdapter<String>(ServiceProviderProfileEdit.this,
                        android.R.layout.simple_spinner_dropdown_item, CategoryInfo.getSubCategories(selection));
                spn_subcategory.setAdapter(selection_subcat);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parentView) {
                    // your code here (optional)
                }

            });

希望它会帮助你......!

//Answer to your comment:

JSONArray data = jsonObject.getJSONArray("LearningStandards");
ArrayAdapter<String> adapter_standard_title = .... //parse standard_title here
spn_title.setAdapter(adapter_standard_title); //set adapter of standard_titles to spinner
//use setOnItemSelectedListener on spn_title
//in onItemSelected you will receive the clicked category
//you can convert it in string using: 
String selected_title = spn_title.getSelectedItem().toString();
//parse ref_id/other req field based on this selected_title