'Table rows inside a fragment not showing

I'm trying to dynamically add a table rows to my table layout to my fragment with a Custom design as follows

      public class StationsFragment extends Fragment {
private TableLayout tableLayout;

public StationsFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View root = inflater.inflate(R.layout.fragment_stations, container, false);
    tableLayout = root.findViewById(R.id.stationsTableLayout);
    KProgressHUD myProgressBar = KProgressHUD.create(getActivity())
            .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
            .setLabel("Loading...\n Please wait.")
            .setCancellable(false)
            .setAnimationSpeed(2)
            .setDimAmount(0.5f);
    retrieveStations(tableLayout, myProgressBar);
    return root;
}

public void retrieveStations(final TableLayout stationsLayout, final KProgressHUD myProgressBar){
    //Check internet connection
    myProgressBar.show();
    StringRequest stationsStringRequest = new StringRequest(Request.Method.POST, NetworkConstants.URL_PRICE_LIST,
            response -> {
                Log.d("Products", "retrieveCategories: "+response);
                JSONObject jsonObject;
                try{
                    JSONObject stationsObject = new JSONObject(response);
                    JSONArray stationsArray = stationsObject.getJSONArray("stations");
                    int i =0;
                    if(stationsArray.length()>0){
                        do {
                            jsonObject = stationsArray.getJSONObject(i);
                            Log.d("Products", "retrieveCategories: "+jsonObject);
                            View tableRow = LayoutInflater.from(getActivity()).inflate(R.layout.price_list_item, null, false);
                            TextView from = tableRow.findViewById(R.id.from);
                            TextView to = tableRow.findViewById(R.id.to);
                            TextView cost = tableRow.findViewById(R.id.cost);
                            TextView min = tableRow.findViewById(R.id.min);
                            TextView max = tableRow.findViewById(R.id.max);
                            from.setText(jsonObject.getString("from"));
                            to.setText(jsonObject.getString("to"));
                            cost.setText(jsonObject.getString("cost"));
                            min.setText(jsonObject.getString("min"));
                            max.setText(jsonObject.getString("max"));
                            stationsLayout.addView(tableRow);
                            stationsLayout.invalidate();
                            i++;
                        }
                        while (i<stationsArray.length());
                    }else{
                        myProgressBar.dismiss();
                        Toast.makeText(getActivity(),"No Stations found", Toast.LENGTH_LONG).show();
                    }
                    myProgressBar.dismiss();
                }catch (JSONException e){
                    myProgressBar.dismiss();
                    e.printStackTrace();
                }
            },
            error -> {
                myProgressBar.dismiss();
                Log.d("Error", "Failed with error msg:\t" + error.getMessage());
                Log.d("Error", "Error StackTrace: \t" + Arrays.toString(error.getStackTrace()));
                // edited here
                try {
                    byte[] htmlBodyBytes = error.networkResponse.data;
                    Log.e("Error", new String(htmlBodyBytes), error);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
                Toast.makeText(getActivity(),"Error " + error, Toast.LENGTH_SHORT).show();
            }){
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("api_token", new Db(getActivity()).getToken());
            return params;
        }
    };
    stationsStringRequest.setRetryPolicy(new DefaultRetryPolicy(
            1000*5,
            3,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    RequestQueue categoryRequestQue = Volley.newRequestQueue(getActivity());
    categoryRequestQue.add(stationsStringRequest);
}

}

Here is the custom layout created

      <?xml version="1.0" encoding="utf-8"?>
       <TableRow xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal">
         <TextView
         android:layout_width="0dp"
    android:layout_height="match_parent"
    android:id="@+id/from"
    android:layout_weight="0.25"
    android:gravity="center"
    android:background="@drawable/table_border"/>
<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/to"
    android:layout_weight="0.25"
    android:gravity="center"
    android:background="@drawable/table_border"/>
<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:id="@+id/cost"
    android:layout_weight="0.25"
    android:gravity="center"
    android:background="@drawable/table_border"/>
<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:id="@+id/min"
    android:layout_weight="0.25"
    android:gravity="center"
    android:background="@drawable/table_border"/>
<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:id="@+id/max"
    android:layout_weight="0.25"
    android:gravity="center"
    android:background="@drawable/table_border"/>

My problem is that the rows do not show up in the table even though they seam to be loaded successfully without any errors. What could be wrong or how can I achieve to add the rows to my table?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source