0

I am getting the values using the GET request from the URL. While I am trying to bind the values into the RecyclerView, I get a null value. From the getValue() method, it's returning the null 'si' value. Why?

public class ShipmentLists extends AppCompatActivity {

    List < ShipmentInfo > si;
    RecyclerView recList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycler_lists);
        si = new ArrayList < > ();
        recList = (RecyclerView) findViewById(R.id.cardList);
        LinearLayoutManager lim = new LinearLayoutManager(ShipmentLists.this);
        lim.setOrientation(LinearLayoutManager.VERTICAL);
        recList.setHasFixedSize(true);
        recList.setLayoutManager(lim);
        ShipmentAdapter sa = new ShipmentAdapter(ShipmentLists.this, getData());
        sa.notifyDataSetChanged();
        recList.setAdapter(sa);
    }

    private List < ShipmentInfo > getData() {
        String url = "URL HERE";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).header("Accept", "application/json")
            .header("Content-Type", "application/json").build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                String mMessage = e.getMessage().toString();
                Log.w("failure Response", mMessage);
                call.cancel();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String mMessage = response.body().string();

                try {
                    final JSONArray myResponse = new JSONArray(mMessage);
                    for (int i = 0; i < myResponse.length(); i++) {

                        String receiver = myResponse.getJSONObject(i).getString("receivername");
                        String address = myResponse.getJSONObject(i).getString("AddressLine1");
                        String city = myResponse.getJSONObject(i).getString("city");
                        String phone = myResponse.getJSONObject(i).getString("contactnumber");
                        String shipmentDate = myResponse.getJSONObject(i).getString("date");
                        String status = myResponse.getJSONObject(i).getString("status");
                        si.add(new ShipmentInfo(receiver, address, city, shipmentDate, phone, status));
                        Log.e("SI DATA", "" + si.size());
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }

                Log.e("RESULTS ARE", "" + mMessage);
            }
        });
        Log.e("LIST SIZE", "" + si.size());
        return si;
    }
OhhhThatVarun
  • 3,981
  • 2
  • 26
  • 49
GS7
  • 1
  • 7

1 Answers1

1
client.newCall(request).enqueue(new Callback()

is an asynchronous call. It means, it will execute at the same time in a separate thread. So si will return null before adding response to it

See this

This question and answers give some ideas about async

majurageerthan
  • 2,169
  • 3
  • 17
  • 30