2017-04-19 94 views
0
public class GraphFrameV2 extends Fragment implements AdapterView.OnItemSelectedListener { 
    Integer hoodSelector = 27; 
    ArrayList<HoodData> hoodDataList = new ArrayList<>(); 
    ArrayList<String> hoodList = new ArrayList<>(); 
    ArrayList<HoodData> completeHoodList = new ArrayList<>(); 

    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     String item = parent.getItemAtPosition(position).toString(); 
     // Showing selected spinner item 
     for(int i = 1; i < hoodList.size(); i++) { 
      if (item.equals(hoodList.get(i))) { 
       setHoodId(i + 27); 
       refreshFragment(); 
      } 

     } 
    } 
    public Double calculateMax(Integer year){ 
     Double max = completeHoodList.get(0).getPercentage(); 
     if(year == 2006){ 
      for(Integer z = 0; z < completeHoodList.size(); z ++){ 
       if(completeHoodList.get(z).getYear() == 2006){ 
        if(completeHoodList.get(z).getPercentage() > max){ 
         max = completeHoodList.get(z).getPercentage(); 
        } 
       } 
      } 
     } 
     Toast.makeText(getActivity(), String.valueOf(max), 
       Toast.LENGTH_LONG).show(); 
     return max; 
    } 
    public void calculateMin(Integer year){ 
     Double min = completeHoodList.get(0).getPercentage(); 
     if(year == 2006){ 
      for(Integer z = 0; z < completeHoodList.size(); z ++){ 
       if(completeHoodList.get(z).getYear() == 2006){ 
        if(completeHoodList.get(z).getPercentage() < min){ 
         min = completeHoodList.get(z).getPercentage(); 
        } 
       } 
      } 
     } 
     if(year == 2007){ 
      for(Integer z = 0; z < completeHoodList.size(); z ++){ 
       if(completeHoodList.get(z).getYear() == 2007){ 
        if(completeHoodList.get(z).getPercentage() < min){ 
         min = completeHoodList.get(z).getPercentage(); 
        } 
       } 
      } 
     } 
     if(year == 2008){ 
      for(Integer z = 0; z < completeHoodList.size(); z ++){ 
       if(completeHoodList.get(z).getYear() == 2008){ 
        if(completeHoodList.get(z).getPercentage() < min){ 
         min = completeHoodList.get(z).getPercentage(); 
        } 
       } 
      } 
     } 
     if(year == 2009){ 
      for(Integer z = 0; z < completeHoodList.size(); z ++){ 
       if(completeHoodList.get(z).getYear() == 2009){ 
        if(completeHoodList.get(z).getPercentage() < min){ 
         min = completeHoodList.get(z).getPercentage(); 
        } 
       } 
      } 
     } 
     if(year == 2011){ 
      for(Integer z = 0; z < completeHoodList.size(); z ++){ 
       if(completeHoodList.get(z).getYear() == 2011){ 
        if(completeHoodList.get(z).getPercentage() < min){ 
         min = completeHoodList.get(z).getPercentage(); 
        } 
       } 
      } 
     } 
    } 
    public Double calculateAvg(Integer year){ 
     Double avg = null; 
     Double sum = 0.0; 
     Integer counter = 0; 
     if(year == 2006){ 
      for(Integer z = 0; z < completeHoodList.size(); z++){ 
       if(completeHoodList.get(z).getYear() == 2006){ 
        sum = sum + completeHoodList.get(z).getPercentage(); 
        counter = counter + 1; 
       } 
      } 
     } 
     return Math.round((sum/counter) * Math.pow(10, 1))/Math.pow(10, 1); 

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

    } 
    public void refreshFragment(){ 
     //refreshes the Fragment 
     hoodDataList.clear(); 
     FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
     transaction.detach(this).attach(this).commit(); 
    } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     final View view = inflater.inflate(R.layout.fragment_graph, 
       container, false); 
     //API Request for all the hoods 
     getAllData(); 
     getDataFromApi((GraphView) view.findViewById(R.id.graph)); 
     getDataForSpinner((Spinner) view.findViewById(R.id.spinner)); 
     return view; 
    } 

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

    public void getAllData(){ 
     RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext()); 
     String url= "http://test.dontstealmywag.ga/api/theft_outof_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); 
           completeHoodList.add(new HoodData(jsonObject.getDouble("percentage"), jsonObject.getInt("hood_id"), jsonObject.getInt("year"), jsonObject.getString("hood_name"))); 
          } 

         } catch (JSONException ex){} 

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


     rq.add(stringRequest); 
    } 

    public void getDataFromApi(final GraphView graph) { 
     RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext()); 
     String url= "http://test.dontstealmywag.ga/api/theft_outof_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){} 
         StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graph); 
         LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] { 
           new DataPoint(1, (calculateAvg(2006 /2))), 
           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) 
         }); 
         LineGraphSeries<DataPoint> average = new LineGraphSeries<>(new DataPoint[]{ 
           new DataPoint(1, 16.2), 
           new DataPoint(2, 15.5), 
           new DataPoint(3, 15.2), 
           new DataPoint(4, 15.9), 
           new DataPoint(5, 13.9) 
         }); 

         // set manual x bounds 
         staticLabelsFormatter.setHorizontalLabels(new String[] {"2006", "2007","2008","2009","2011"}); 
         //creates custom x-axis 
         graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter); 
         graph.setTitle("Wijk " + hoodSelector + " - " + hoodDataList.get(0).getHood_name()); 
         //set nice background color :) 
         series.setDrawBackground(true); 
         //shows points at datapoints 
         series.setDrawDataPoints(true); 
         average.setDrawDataPoints(true); 
         //size of the points 
         series.setDataPointsRadius(10.0f); 
         series.setOnDataPointTapListener(new OnDataPointTapListener() { 
          @Override 
          public void onTap(Series series, DataPointInterface dataPoint) { 
           Toast.makeText(getActivity(), "Punt "+ dataPoint, Toast.LENGTH_SHORT).show(); 
          } 
         }); 
         series.setTitle("%"); 
         series.setAnimated(true); 
         average.setAnimated(true); 
         average.setColor(Color.argb(255, 0, 51, 153)); 
         average.setThickness(5); 
         series.setColor(Color.argb(255, 102, 204, 255)); 
         graph.addSeries(average); 
         graph.getLegendRenderer().setVisible(true); 
         graph.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP); 
         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/theft_outof_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.setPrompt("Title"); 
         spinner.setAdapter(adapter); 

         spinner.setOnItemSelectedListener(GraphFrameV2.this); 


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


     rq.add(stringRequest); 
    } 


} 

問題:計算最高不工作

我寫這個代碼。當我在裏面調用calculateMax(2006) getAllData時,它工作正常,並且給我我想要的祝酒,但是當我在getDataFromApi中調用它時,應用程序崩潰告訴我我的列表不包含任何項目。

這很奇怪,因爲我的calculateAvg()方法確實有效。

任何人都可以幫我解決這個問題嗎?

+1

請添加堆棧跟蹤 – reynev

+0

你有沒有注意到你的'if's裏面所有的代碼都是完全一樣的? – GVillani82

+0

@ GVillani82是的,但我不知道如何以另一種方式做到這一點 –

回答

1

問題與網絡請求本質上是異步有關。

假設有3個同步動作(A,B,C)

Start A 
Complete A 
Start B 
Complete B 
Start C 
Complete C 

如果B是異步的,可能發生

Start A 
Complete A 
Start B (async) 
Start C (maybe C need the result from B, but unfortunately B is still running, on another thread) 
Complete C 
Complete B 

所以,你基本上要等到異步操作如果您的下一個操作需要結果,則完成。

爲了做到這一點,您需要在回調onResponse()中執行您的下一個動作。只有這裏有數據可用。

0

可以通過使用completeHoodList.get(z).getYear() == year

設定例大大簡化calculateMin & calcualteMax方法:

public Double calculateMin(Integer year){ 
    Double min = Intenger.MAX_VALUE; // Or whatever default value if the list is size 0 
    for(Integer z = 0; z < completeHoodList.size(); z ++){ 
     if(completeHoodList.get(z).getYear() == year){ 
      if(completeHoodList.get(z).getPercentage() < min){ 
       min = completeHoodList.get(z).getPercentage(); 
      } 
     } 
    } 
    return min; 
} 

你甚至可以採取一步通過使用for-each循環,而不是一個for循環。

例子:

public Double calculateMin(Integer year){ 
    Double min = Intenger.MAX_VALUE; // Or whatever default value if the list is size 0 
    for(HoodData hoodData : completeHoodList){ 
     if(hoodData.getYear() == year){ 
      if(hoodData.getPercentage() < min){ 
       min = hoodData.getPercentage(); 
      } 
     } 
    } 
    return min; 
}