无法在Spinner的所选项目上刷新ListView以从url获取新数据

时间:2018-03-21 12:11:35

标签: android json listview refresh spinner

  1. 我无法刷新微调器项目选择的列表视图。
  2. 我想根据微调器的项值构建新的url,以获取新的json数据并转换为listview。
  3. 您还可以帮我讲述如何创建方法来传递新网址以获取新数据。
  4. 主要问题与标题相同 您的每一个帮助将不胜感激。

    @覆盖 public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){     //为此片段扩充布局     查看view = inflater.inflate(R.layout.fragment1,container,false);

    String [] array_values = {"All "," 5"," 4"," 3"," 2"," 1"};
    final Spinner spinner = view.findViewById(R.id.spinner);
    
    final ArrayAdapter<String> spinner_adapter = new ArrayAdapter<>(this.getActivity(), android.R.layout.simple_spinner_item, array_values);
    spinner_adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    spinner.setAdapter(spinner_adapter);
    
    final String spinnerValue = spinner.getSelectedItem().toString();
    url = "http://website.com/demo.php?pswd=kvpswd";
    
    listView = (ListView) view.findViewById(R.id.demo_list_view);
    adapter = new CustomListAdapter(this.getActivity(), List);
    listView.setAdapter(adapter);
    
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
          //  listView.setAdapter(null);
          String spinnerValue = spinner.getSelectedItem().toString();
            url = "http://website.com/demo.php?pswd="+spinnerValue;
            adapter.notifyDataSetChanged();
        }
        public void onNothingSelected(AdapterView<?> parent) {
    
        }
    });
    
    // Creating volley request obj
    JsonArrayRequest Requset = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();
    
                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {
    
                            JSONObject obj = response.getJSONObject(i);
    
                            DataFetcher datafetcher = new DataFetcher();
    
                            datafetcher.set1(obj.getString("item1"));
                            datafetcher.set2(obj.getString("item2"));
                            datafetcher.set3(obj.getString("item3"));
                            datafetcher.set4(obj.getString("item4"));
                            datafetcher.set5(obj.getString("item5"));
                            datafetcher.set6(obj.getString("item6"));
                            datafetcher.set7(obj.getString("item7"));
                            datafetcher.set8(obj.getString("item8"));
    
                            // adding list to list array
                            List.add(datafetcher);
    
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
    
                    }
    
                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            hidePDialog();
    
        }
    });
    

    }

1 个答案:

答案 0 :(得分:0)

为api调用make方法并传递url并将数据设置到列表视图中。 下面的方法进入微调器选择时间。

 private void callApiData(String url){
    // Creating volley request obj
    JsonArrayRequest Requset = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);

                            DataFetcher datafetcher = new DataFetcher();

                            datafetcher.set1(obj.getString("item1"));
                            datafetcher.set2(obj.getString("item2"));
                            datafetcher.set3(obj.getString("item3"));
                            datafetcher.set4(obj.getString("item4"));
                            datafetcher.set5(obj.getString("item5"));
                            datafetcher.set6(obj.getString("item6"));
                            datafetcher.set7(obj.getString("item7"));
                            datafetcher.set8(obj.getString("item8"));

                            // adding list to list array
                            List.add(datafetcher);
                            setAdapter();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            hidePDialog();

        }
    });
}

并为适配器设置制作一个单独的方法。

  private void setAdapter(){
    if(adpter==null){
        adapter = new CustomListAdapter(this.getActivity(), List);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
    else{
        adapter.notifyDataSetChanged();
    }
}