我正在尝试使用Fragment类,它们都工作正常,但是当尝试使用从服务器获取的变量时,fragment不会更新值。
这是我的代码:
public class report_fragment extends Fragment {
private BarChart barChart;
private ListView list;
public RequestQueue queue;
private Float ris;
String cu;
Button bSave;
private String userID,email,name,surname,age,privilegi;
TextView tExer, tRis;
String risultato, disciplina;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//just change the fragment_dashboard
//with the fragment you want to inflate
//like if the class is HomeFragment it should have R.layout.home_fragment
//if it is DashboardFragment it should have R.layout.fragment_dashboard
final View view = inflater.inflate(R.layout.fragment_report_fragment, container, false);
barChart = (BarChart) view.findViewById(R.id.bargraph);
tExer = (TextView) view.findViewById(R.id.tExerc);
tRis = (TextView) view.findViewById(R.id.tRis);
Bundle b = this.getArguments();
if (b != null) {
name = b.getString("name");
surname = b.getString("surname");
age = b.getString("age");
privilegi = b.getString("privilegi");
userID = b.getString("user");
}
Log.d("iduser2", "" + userID);
graficaDati(userID);
Last(userID);
tExer.setText(disciplina);
tRis.setText(risultato);
return view;
}
private void graficaDati(final String ID) {
//final TextView gTextView = (TextView) findViewById(R.id.graph);
// gTextView.append(" entrato in Data");
String url = "https://anto-mc.000webhostapp.com/atleti_statistiche2.php";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
final ArrayList<BarEntry> barEntries = new ArrayList<>();
ArrayList<String> theDates = new ArrayList<>();
JSONArray jsonArray = new JSONArray(response);
for (int i = 0 ; i < jsonArray.length(); i++) {
JSONObject out = jsonArray.getJSONObject(i);
String meteo = out.getString("meteo");
String dataa = out.getString("data");
String risultato = out.getString("risultato");
String score = out.getString("punteggio");
String tot= out.getString("tot");
Log.d("quiqui", ""+tot);
barEntries.add(new BarEntry(i, Float.parseFloat(tot)));
theDates.add(dataa+"-"+meteo);
}
BarDataSet set1;
set1 = new BarDataSet(barEntries, "Your score");
set1.setColors(new int[]{Color.parseColor("#ffffff")});
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
XAxis xAxis = barChart.getXAxis();
xAxis.setValueFormatter(new MyCustomXAxisValueFormatter(theDates));
xAxis.setGranularity(1f);
data.setValueTextSize(10f);
data.setBarWidth(0.9f);
barChart.setData(data);
barChart.getAxisRight().setEnabled(false);
barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
barChart.invalidate();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
})
{
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("userID", ID);
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(getActivity());
queue.add(postRequest);
}
private void Last(final String ID) {
//final TextView gTextView = (TextView) findViewById(R.id.graph);
// gTextView.append(" entrato in Data");
Log.d("ciaoo","sei qui");
String url = "https://anto-mc.000webhostapp.com/last.php";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
final ArrayList<BarEntry> barEntries = new ArrayList<>();
ArrayList<String> theDates = new ArrayList<>();
Log.d("ciaoo","sei qui2");
JSONArray jsonArray = new JSONArray(response);
for (int i = 0 ; i < jsonArray.length(); i++) {
JSONObject out = jsonArray.getJSONObject(i);
Log.d("ciaoo","sei qui2" +""+out);
String meteo = out.getString("meteo");
disciplina = out.getString("disciplina");
risultato = out.getString("risultato");
String score = out.getString("punteggio");
String tot= out.getString("tot");
Log.d("queryris",""+disciplina + " "+ risultato);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
})
{
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("userID", ID);
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(getActivity());
queue.add(postRequest);
}
}
我收到了变量“ risultato and disciplina”,并且在第二次浏览该片段后才看到正确的值。
第一时间,我看到了null变量。
我尝试使用该片段的刷新,但是它不起作用。
条形图正常工作,但是setText
不起作用。我尝试做类似失败的条形图一样。
有人有建议吗?谢谢。
答案 0 :(得分:0)
您需要在onResponse
函数中设置文本。在网络调用完成之前,这些值不可用。因此,onResponse
函数内部的Last
函数应如下所示。
private void Last(final String ID) {
//final TextView gTextView = (TextView) findViewById(R.id.graph);
// gTextView.append(" entrato in Data");
Log.d("ciaoo","sei qui");
String url = "https://anto-mc.000webhostapp.com/last.php";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
final ArrayList<BarEntry> barEntries = new ArrayList<>();
ArrayList<String> theDates = new ArrayList<>();
Log.d("ciaoo","sei qui2");
JSONArray jsonArray = new JSONArray(response);
for (int i = 0 ; i < jsonArray.length(); i++) {
JSONObject out = jsonArray.getJSONObject(i);
Log.d("ciaoo","sei qui2" +""+out);
String meteo = out.getString("meteo");
disciplina = out.getString("disciplina");
risultato = out.getString("risultato");
String score = out.getString("punteggio");
String tot= out.getString("tot");
Log.d("queryris",""+disciplina + " "+ risultato);
}
// Set the text here when it is available
tExer.setText(disciplina);
tRis.setText(risultato);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
})
{
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("userID", ID);
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(getActivity());
queue.add(postRequest);
}
关键思想是在API调用返回一些值之后,在这些值可用之后设置文本。当您调用服务器API时,它正在UI线程以外的另一个线程中执行。它不会像您期望的那样在onCreateView
函数中停止执行。
希望有帮助。