【问题标题】:How to calculate total jumlahtrx in textView which get from json如何计算从json获取的textView中的总jumlahtrx
【发布时间】: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' 是什么意思?

标签: php android json


【解决方案1】:

在活动级别(不在 AsyncTask 内部)移动您的 sum 变量:

public class YourActivity extends ...{
   int sum = 0;

然后对值求和

  String payment = jsonObject.getString("jumlahtrx");
                                Totaljumlah = Integer.parseInt(payment);
                                sum = sum+Totaljumlah;

最后,在你的文本视图中设置值:

        TextView textViewTotal = (TextView)findViewById(R.id.textViewTotal);
        textViewTotal.setText("" + sum);

【讨论】:

  • 很高兴为您提供帮助
  • 我可以问一个问题吗?我们可以有一个“if”语句,例如 if "jenisrx = masuk" then "sum = sum+Totaljumlah" elseif "jenisrx = keluar" then "sum = sum-Totaljumlah" 。我们可以吗?
  • 可以,但你需要知道如何在java中比较字符串
  • 谢谢,我在 sum 内部创建了它,它的工作,谢谢
  • 我想,我们这里有一些问题,当我尝试添加更多数据库时它没有显示,只有 2 个数据显示并更改“sum”值,有什么解决办法吗?
【解决方案2】:

首先,你要使用

json_decode($yourresponse)

检索数组。

然后,你用得到

$1 = $yourresponse[0]["jumlahtrx"]
$2 = $yourresponse[1]["jumlahtrx"]; 

也许

bcadd($1,$2); 

等等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多