• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Why the arrayList return empty with volley response?

Marvix

Newbie
Hello,

The carList is declared as a public variable in the class, and the log shows that values are added in the array, yet when I call the list is empty, how to solve it with Volley response?

The arratList instance is created before onResponse, the method which is using the array is added after the RequestQueueSingleton method.

Java:
public void onResponse(JSONObject response) {
try {
    JSONArray resArray = response.getJSONArray("result");

    for(int i=0;i<resArray.length();i++) {
        JSONObject car = resArray.getJSONObject(i);
        String id = car.getString("id");
        String brand = car.getString("brand");
        String created = car.getString("created");

        carList.add(new CarListItem(brand, plate_number));
        Log.d(TAG, "ADDED___: " +brand + " " + plate_number);
    }

Thanks,
 
So are you saying that items are being added to the carList?
I don't quite understand what you mean by "when I call the list is empty".
 
I found the solutions. The items are added to the carList, but when I call the carList out of onResponse returns an empty array, so I moved the called method to the body of the onResponse, and that solved the issue.
But why?
 
Show the complete code, including the point at which you declare variable carList, and I can probably explain.
 
Sorry for delayed reply.
Actually, the arrayList is used to make a RecyclerView, the issue that and the RecyclerView is called our of the onResponse body will get an empty arrayList, so I was forced to use within the onResponse body.

But using the RecyclerView within the onResponse body showing and error
E/RecyclerView: No adapter attached; skipping layout
, but the RecyclerView is working fine.

Java:
    public void buildRecyclerView() {
        mRecyclerView = findViewById(R.id.carListView);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);


        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new RecyclerAdapterCarsList.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                WebServiceUtils.log(getBaseContext(), TAG, "______ Clicked: " + position);

                mCarId = carList.get(position).getCarId();
                mPlateNumber = carList.get(position).getPlateNumber();
                mBrand = carList.get(position).getBrand();
                mColor = carList.get(position).getColor();

                SharedPreferenceUtils carData = SharedPreferenceUtils.getInstance(getApplicationContext());
                carData.setValue("carId", mCarId);
                carData.setValue("pLateNumber", mPlateNumber);
                carData.setValue("brand", mBrand);
                carData.setValue("color", mColor);

                WebServiceUtils.log(getBaseContext(), TAG, "______ Car data saved in carData.");
                sendDataConfirmationDialog(mCarId, mPlateNumber, mBrand, mColor);
            }
        });
        WebServiceUtils.log(getBaseContext(), TAG, "______ ArrayList is ready!");
    }

The OnResponse:

Java:
 public void onResponse(JSONObject response) {
                        dialog.dismiss();

                        try {
                            JSONArray resArray = response.getJSONArray("result");
                            WebServiceUtils.log(getBaseContext(), TAG, "_____ Result: " + resArray);

                            for (int i = 0; i < resArray.length(); i++) {
                                JSONObject car = resArray.getJSONObject(i);

                                int car_id = car.getInt("id");
                                String brand = car.getString("brand");
                                int plateNumber = car.getInt("plate_number");
                                String color = car.getString("color");
                                int created = car.getInt("created");

                                carList.add(new CarListItem(car_id, brand, plateNumber, color));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        if (carList.size() > 0) {
                            mAdapter = new RecyclerAdapterCarsList(carList);
                            buildRecyclerView();
                        }
                    }
 
The problem that I'm getting an error "E/RecyclerView: No adapter attached; skipping layout". The RecyclerView should be placed in onCreate body, but I can not do this without check the size of the array list, and the array list is only available inside the onResponse.

My code is working fine, but still showing an error as mentioned above.
 
The problem that I'm getting an error "E/RecyclerView: No adapter attached; skipping layout". The RecyclerView should be placed in onCreate body, but I can not do this without check the size of the array list, and the array list is only available inside the onResponse.

My code is working fine, but still showing an error as mentioned above.

Set the view adapter in the onConnected() method, as suggested here (top hit on Google for your error message btw)

https://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout
 
I know this top hit on Google, but different scenarios ;)

I don't have onConnected() to override it in my class: public class CarListActivity extends AppCompatActivity
 
Is this causing your application to crash, or problems displaying your data in the RecyclerView?
 
is causing nothing, all fine, should I ignore it this case?

;) I honestly wouldn't worry about it.
You have the setAdapter in the wrong place in your code, but difficult to say where to place it, as your code fragments don't provide the full picture.
If your app runs, and shows the right data though, I wouldn't be too concerned.
 
Back
Top Bottom