【发布时间】:2017-11-26 00:53:10
【问题描述】:
[{"id":"4","jenisrx":"masuk","keterangantrx":"masssuakn","jumlahtrx":"50000","tgltrx":"2017-11-25 23:45 :06"},{"id":"5","jenisrx":"keluar","keterangantrx":"keluar","jumlahtrx":"20000","tgltrx":"2017-11-25 23: 45:27"},{"id":"6","jenisrx":"keluar","keterangantrx":"biaya","jumlahtrx":"5000","tgltrx":"2017-11-26 08 :22:38"}]
我想计算从 php 获得的总“jumlahtrx”总和,我正在使用 JsonArray。
在我的班级中名列前茅
int Saldo = 0;
然后
@Override
protected Void doInBackground(Void... arg0)
{
// Passing HTTP URL to HttpServicesClass Class.
HttpServicesClass httpServicesClass = new HttpServicesClass(HttpUrl);
try
{
httpServicesClass.ExecutePostRequest();
if(httpServicesClass.getResponseCode() == 200)
{
JSonResult = httpServicesClass.getResponse();
if(JSonResult != null)
{
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(JSonResult);
int sumTotaljumlah = 0;
JSONObject jsonObject;
Student student;
studentList = new ArrayList<Student>();
for(int i=0; i<jsonArray.length(); i++)
{
student = new Student();
jsonObject = jsonArray.getJSONObject(i);
//String jenis = jsonObject.getString("jenistrx");
String total = jsonObject.getString("jumlahtrx");
sumTotaljumlah = Integer.parseInt(total);
Saldo = Saldo + sumTotaljumlah;
// Adding Student Id TO IdList Array.
IdList.add(jsonObject.getString("id").toString());
//Adding Student Name.
student.JenisTrx = jsonObject.getString("jenistrx").toString();
student.JumlahTrx = jsonObject.getString("jumlahtrx").toString();
studentList.add(student);
}
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else
{
Toast.makeText(context, httpServicesClass.getErrorMessage(), Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
这里是如何显示的
@Override
protected void onPostExecute(Void result)
{
progressBar.setVisibility(View.GONE);
StudentListView.setVisibility(View.VISIBLE);
TextView textViewTotal = (TextView)findViewById(R.id.textViewTotal);
textViewTotal.setText("Saldo: Rp. "+Saldo);
ListAdapterClass adapter = new ListAdapterClass(studentList, context);
StudentListView.setAdapter(adapter);
}
有什么解决办法吗?
【问题讨论】:
-
java 基础:变量的作用域...
-
你为什么使用“String payment = c.getString("jumlahtrx");” , 'c' 是什么意思?