2017-04-12 54 views
0
public class GraphFrame extends Fragment implements AdapterView.OnItemSelectedListener { 
    Integer hoodSelector = 27; 
    ArrayList<HoodData> hoodDataList = new ArrayList<>(); 
    ArrayList<String> hoodList = new ArrayList<>(); 
    Context mContext; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     final View view = inflater.inflate(R.layout.fragment_graph, 
       container, false); 
     setHoodId(34); 


     //API Request for all the hoods 


     getDataFromApi((GraphView) view.findViewById(R.id.graph)); 
     getDataForSpinner((Spinner) view.findViewById(R.id.spinner)); 
     return view; 
    } 

    public void setHoodId(Integer hood){ 
     hoodSelector = hood; 
    } 

    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     String item = parent.getItemAtPosition(position).toString(); 


     // Showing selected spinner item 
     Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show(); 
    } 


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

    } 
    public void getDataFromApi(final GraphView graph) { 
     RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext()); 
     String url= "http://test.dontstealmywag.ga/api/damage_or_theft_car_wijk.php?hood_id=" + hoodSelector; 
     StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         // Do something with the response 
         try{ 
          JSONObject o = new JSONObject(response); 
          JSONArray values=o.getJSONArray(""); 
          for (int i = 0; i < values.length(); i++) { 
           JSONObject jsonObject = values.getJSONObject(i); 
           hoodDataList.add(new HoodData(jsonObject.getDouble("percentage"), jsonObject.getInt("hood_id"), jsonObject.getInt("year"), jsonObject.getString("hood_name"))); 
          } 

         } catch (JSONException ex){} 
         LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] { 
           new DataPoint(1, hoodDataList.get(0).getPercentage()), 
           new DataPoint(2, hoodDataList.get(1).getPercentage()), 
           new DataPoint(3, hoodDataList.get(2).getPercentage()), 
           new DataPoint(4, hoodDataList.get(3).getPercentage()), 
           new DataPoint(5, hoodDataList.get(4).getPercentage()) 
           //new DataPoint(12, 0) 
         }); 
         graph.setTitle("Wijk " + hoodSelector + " - " + hoodDataList.get(0).getHood_name()); 
         StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graph); 

         // set manual x bounds 
         staticLabelsFormatter.setHorizontalLabels(new String[] {"2006", "2007","2008","2009","2011"}); 
         //creates custom x-axis 
         graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter); 
         //set nice background color :) 
         series.setDrawBackground(true); 
         //shows points at datapoints 
         series.setDrawDataPoints(true); 
         //size of the points 
         series.setDataPointsRadius(10.0f); 
         graph.addSeries(series); 



        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         // Handle error 
        } 
       }); 


     rq.add(stringRequest); 
    } 
    public void getDataForSpinner(final Spinner spinner){ 
     RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext()); 
     String url= "http://test.dontstealmywag.ga/api/damage_or_theft_car_wijk.php"; 
     StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         // Do something with the response 
         try{ 
          JSONObject o = new JSONObject(response); 
          JSONArray values=o.getJSONArray(""); 
          for (int i = 0; i < values.length(); i++) { 
           JSONObject jsonObject = values.getJSONObject(i); 
           if (!hoodList.contains(jsonObject.getString("hood_name"))) { 
            hoodList.add(jsonObject.getString("hood_name")); 
           } 
          } 
         } catch (JSONException ex){} 
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(spinner.getContext(), android.R.layout.simple_spinner_item, hoodList); 
         // Specify the layout to use when the list of choices appears 
         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
         // Apply the adapter to the spinner 
         spinner.setAdapter(adapter); 
         spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
          @Override 
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
           setHoodId(27); 
          } 
         }); 


        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         // Handle error 
        } 
       }); 


     rq.add(stringRequest); 
    } 
} 

我想將我的hood_id設置爲另一個值,當我點擊一個項目內的微調,所以我可以創建不同的圖形,但不知何故這是行不通的。任何人都可以讓我在正確的方向或幫助我解決這個問題。微調OnItemClickListener

+0

你需要在字符串的xml文件中添加陣列 – Lokesh

+0

向我們展示你的字符串XML文件 – Lokesh

+0

@Lokesh對不起我hitted輸入方法快。這個問題是不正確的。現在它是 –

回答

0

您需要在微調器上設置AdapterView.OnItemSelectedListener。取而代之的

spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
          @Override 
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
           setHoodId(27); 
          } 
         }); 

嘗試

spinner.setOnItemSelectedListener(GraphFrame.this); 
+0

現在我的圖不繪製。這可能是因爲OnItemSelectedListener方法不在我的API調用中調用。有任何解決這個問題的方法嗎 –