美好的一天, 我一直试图解决这个问题好几天了。我试图获取一些json数据并将其作为MPandroidChart库的饼图条目。如果我只是将它放在onCreate方法中并且手动提供输入而没有像这样的凌空,那么该库可以正常工作:
pie_entry.add(new PieEntry(5, "test"));
但是,我把它放在凌空的onResponse方法中,或者如果我尝试获取条目并将其发送到另一个方法,然后将其添加到pieChart,或者如果我尝试将条目添加到全局可验证然后添加到图表它不起作用。这是我的代码:
String tag_String_reg = "reg_GetVoteResults";
db = new SQLiteHandler(getApplicationContext());
final HashMap<String, Object> myData;
myData = db.getUserIDAndOrg();
int userID = (int) myData.get(db.KEY_USER_ID);
final String mOrg = (String) myData.get(db.KEY_USER_UID);
StringRequest stringRequest = new StringRequest(Request.Method.POST,
AppConfig.URL_GET_VOTERESULTS, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray JArray = new JSONArray(response);
for (int i = 0; i < JArray.length(); i++) {
JSONObject answerInfo = JArray.getJSONObject(i);
String answer = answerInfo.getString("answer");
float vote = answerInfo.getInt("answer_vote");
//vote_count = new ArrayList<>();
//anslist = new ArrayList<>();
//vote_count.add(vote);
//anslist.add(answer);
pie_entry.add(new PieEntry(vote, answer));
}
pie_entry.add(new PieEntry(5, "test"));
pie_entry.add(new PieEntry(7, "test2"));
Log.wtf("current name and int ", pie_entry.toString());
//testing(pie_entry);
//Log.wtf("final result: ", pe.toString());
pieChart.setUsePercentValues(true);
//Log.wtf("inside chart ", pe.toString());
pieChart.getDescription().setEnabled(false);
pieChart.setExtraOffsets(5, 10, 5, 5);
pieChart.setDragDecelerationFrictionCoef(0.95f);
pieChart.setDrawHoleEnabled(true);
pieChart.setHoleColor(WHITE);
pieChart.setTransparentCircleRadius(61f);
PieDataSet dataSet = new PieDataSet(pie_entry, "Voting Results");
dataSet.setSliceSpace(3f);
dataSet.setSelectionShift(5f);
dataSet.setColors(ColorTemplate.JOYFUL_COLORS);
PieData data = new PieData((dataSet));
data.setValueTextSize(10f);
data.setValueTextColor(Color.YELLOW);
pieChart.setData(data);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Didnt work. Catch. "+ response.toString(), Toast.LENGTH_LONG).show();
Log.d(TAG, "Didnt work. Error : "+e.toString());
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if(error.getMessage()==null){
Toast.makeText(getApplicationContext(), " ERROR ", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Error. Please check your internet connection. ErrorListener. " + error.getMessage(), Toast.LENGTH_LONG).show();
}
}
}){
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("org_id", mOrg);
params.put("question_id", question_id);
return params;
}
};
AppController.getInstance().addToRequestQueue(stringRequest, tag_String_reg);`
我可能json响应有效,因为我用Log.wtf
检查了它
任何帮助将不胜感激。
此外,logcat中没有错误,只显示我的Log.wtf的结果:
current name and int: [Entry, x: 0.0 y: 1.0, Entry, x: 0.0 y: 1.0, Entry, x: 0.0 y: 5.0, Entry, x: 0.0 y: 7.0]
答案 0 :(得分:1)
你没忘记
吗?pieChart.invalidate();
之后
pieChart.setData(data);
强制图表刷新?
答案 1 :(得分:0)
在pieChart.setData(data)
之后添加这些:
pieChart.notifyDataSetChanged();
pieChart.invalidate();
您需要通知图表视图,了解基础数据的更改,就像我们在数据更改时使用RecyclerView.Adapter
一样。