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 void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
if(item == "Tarwewijk"){
setHoodId(27);
}
else if(item == "Carnisse"){
setHoodId(28);
//refreshFragment();
//getDataFromApi((GraphView) view.findViewById(R.id.graph));
}
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public void refreshFragment(){
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
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 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){
setHoodId(28);
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.setOnItemSelectedListener(GraphFrame.this);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
});
rq.add(stringRequest);
}
}
我的代码的一些解释:我从在线API生成的数据创建图表视图。现在我有多个引擎盖来创建图表。我制作了一个旋转器,其中包含我API中的所有引擎盖。现在,当我按下微调项时,我希望它能够更改从API中提取的值。这可以通过更改hoodId来完成。因此,如果HoodID = 27,则会给出另一个图表,如果HoodID = 28。
所以我认为是从onItemSelectedListener中的微调器中获取值,并创建类似
的条件if(item == "Tarwewijk"){
setHoodId(28);
}
这不会以某种方式起作用。任何人都可以帮我解决我面临的问题。