简体   繁体   中英

adding data from mysql database to tableLayout using volley

I am trying to add data to my tableLayout using volley as follows

    private void getData() {
    final ArrayList <Detail> drinks = new ArrayList<>();
    final RotatingCircularDotsLoader loader = new RotatingCircularDotsLoader(getActivity(),
            20, 60, ContextCompat.getColor(getActivity(), R.color.red));
    loader.setAnimDuration(3000);
    content.addView(loader);
    StringRequest drinkStringRequest = new StringRequest(Request.Method.GET, AppConfig.URL_DETAILS,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject jsonObject;
                    try{
                        JSONObject drinkObject = new JSONObject(response);
                        JSONArray drinkArray = drinkObject.getJSONArray("data");
                        for(int i=0; i<drinkArray.length();i++){
                            jsonObject = drinkArray.getJSONObject(i);
                            tableRow = LayoutInflater.from(getActivity()).inflate(R.layout.table_item, null, false);
                            //table components
                            id = tableRow.findViewById(R.id.id);
                            name = tableRow.findViewById(R.id.name);
                            ppn = tableRow.findViewById(R.id.ppn);
                            spn = tableRow.findViewById(R.id.spn);
                            email = tableRow.findViewById(R.id.email);
                            idno = tableRow.findViewById(R.id.idNo);
                            btype = tableRow.findViewById(R.id.btype);
                            loc = tableRow.findViewById(R.id.loc);
                            monthly = tableRow.findViewById(R.id.monthly);
                            status = tableRow.findViewById(R.id.status);
                            created = tableRow.findViewById(R.id.created);
                            id.setText(String.valueOf(jsonObject.getInt("id")));
                            name.setText(jsonObject.getString("name"));
                            ppn.setText(jsonObject.getString("primaryphone"));
                            spn.setText(jsonObject.getString("secondaryphone"));
                            email.setText(jsonObject.getString("email"));
                            idno.setText(String.valueOf(jsonObject.getInt("idno")));
                            btype.setText(jsonObject.getString("businesstype"));
                            loc.setText(jsonObject.getString("location"));
                            monthly.setText(jsonObject.getString("monthlyrevenue"));
                            created.setText(jsonObject.getString("reg_date"));
                            status.setText(String.valueOf(jsonObject.getInt("status")));
                            items.addView(tableRow);
                        }
                        content.removeView(loader);

                    }catch (JSONException e){
                        content.removeView(loader);
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    content.removeView(loader);
                    error.printStackTrace();
                }
            });
    RequestQueue requestQue = Volley.newRequestQueue(getActivity());
    requestQue.add(drinkStringRequest);
}

The program runs with no errors but the rows are not added despite there being data in the database that is being fetched and when I debug each individual jsonObject value I get something ie Log.d(TAG, "onResponse: "+jsonObject.getString("monthlyrevenue")); gives 45000 in the console but the row is empty. What could I be dooing wrong or rather how can I manage to add the data to the table?

This is my json response

    {"data":[{"id":1,"name":"test","primaryphone":"0712345678","secondaryphone":"0723456789","email":"test@test.com","idno":123456789,"businesstype":"test","location":"test","monthlyrevenue":"45000","regdate":"2020-09-21 11:20:05","updatedate":null,"status":1}]}

Looking at your JSON response the third last object name is regdate without underscore but in your created.setText() method, you have referred to it using the name reg_date with underscore. Correcting that will solve your problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM