2

I'm using Adapter to introduce my data from the DB to a layout - kind of inbox page. when I debuegging i see in my console the Error "no adapter attached, skipping layout" i think it happens because the line: <android.support.v7.widget.RecyclerView is not working. (that line is in the XML) i understand that this tag is no more working in android and for the new version i should put this line: <androidx.recyclerview.widget.RecyclerView but that's not working either. someone maybe know how should i fix this?

put my code here just in case.. InboxDoctor.JAVA

public class InboxDoctor extends AppCompatActivity {
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.loginregister.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.time.Clock;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

        private RecyclerView recyclerView;
        private AdapterActivity adapter;
        private List<ModelActivity> modelActivityList;
        private RelativeLayout relativeLayout;
        private HashMap<Integer,String> repMap;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.inbox_doctor);

            relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
            recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));

            modelActivityList = new ArrayList<>();
            StringRequest stringRequest = new StringRequest(Request.Method.POST,
                    URLs.URL_INBOXDR,
                    //lambda expression
                    response -> {

                        relativeLayout.setVisibility(View.GONE);
                        //repMap = new HashMap<Integer,String>();

                        try {
                            JSONObject repObj = new JSONObject(response);
                            JSONArray repArray = repObj.getJSONArray("report");
                            JSONObject jObg = new JSONObject();
                            ModelActivity modelActivity = new ModelActivity();

                            for(int i=0; i<=repArray.length(); i++){

                                jObg = repArray.getJSONObject(i);
                                modelActivity.setId(jObg.getInt("id"));
                                modelActivity.setSentTime(jObg.getString("sent_time"));
                                modelActivity.setPatientId(jObg.getString("patient_id"));
                                modelActivity.setTestName(jObg.getString("name"));
                                modelActivity.setUrgent(jObg.getInt("is_urgent"));
                                modelActivityList.add(modelActivity);
                                System.out.println(modelActivityList.get(i).getId());
                            }

                            adapter = new AdapterActivity(modelActivityList,getApplicationContext());
                            recyclerView.setAdapter(adapter);


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

                    },
                    //lambda expression
                    error -> Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show())
            {
                @Nullable
                @Override
                protected Map<String,String> getParams() throws AuthFailureError {
                    Map<String,String> params = new HashMap<>();
                    params.put("department", String.valueOf(SharedPrefManager.getInstance(getApplicationContext()).getUser().getDepartment()));
                    System.out.println(params.get("department"));
                    return params;
                }
            };
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);
        }






}

AdapterActivity.JAVA:

package il.reportap;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.loginregister.R;

import java.util.List;
import java.util.Random;

public class AdapterActivity extends RecyclerView.Adapter<AdapterActivity.ViewHolder>{
    private List<ModelActivity> modelActivityList ;
    private Context context;

    public AdapterActivity(List<ModelActivity> modelActivityList, Context context) {
        this.modelActivityList = modelActivityList;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row_layout,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ModelActivity modelActivity = modelActivityList.get(position);

        holder.sentTime.setText(modelActivity.getSentTime());
        holder.patientId.setText(modelActivity.getPatientId());
        holder.testName.setText(modelActivity.getTestName());
        holder.isUrgent.setText(modelActivity.getIsUrgent());
    }

    @Override
    public int getItemCount() {
        return modelActivityList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        public TextView sentTime, patientId,testName,isUrgent;

        public ViewHolder(View itemView) {
            super(itemView);

            sentTime = (TextView)itemView.findViewById(R.id.sentTime);
            patientId = (TextView)itemView.findViewById(R.id.patientId);
            testName = (TextView)itemView.findViewById(R.id.testName);
            isUrgent = (TextView)itemView.findViewById(R.id.isUrgent);
        }
    }
}

the piece of code in inbox_doctor.xml:

           <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    tools:context="il.reportap.InboxDoctor">

                    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                        xmlns:tools="http://schemas.android.com/tools"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/relativeLayout"
                        tools:context="abhiandroid.com.jsonparsingexample.MainActivity">

                        <androidx.recyclerview.widget.RecyclerView
                            android:id="@+id/recyclerView"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent" />
                    </RelativeLayout>

                </LinearLayout>

            </ScrollView>

thank you for your help!

Yael A.
  • 21
  • 2
  • Is anything [here](https://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout) helpful? – lucidbrot Apr 03 '21 at 11:17
  • 1
    I've already seen it. unfortunately not :( – Yael A. Apr 03 '21 at 12:09
  • Hmm I think I can't help here, but two ideas: 1) is `setAdapter` actually called? Maybe there is an exception in the try catch block that skips over that part. 2) from the link before, there is [this answer](https://stackoverflow.com/a/35204976/2550406) which sounds like it's worth trying what happens if you don't have the recyclerview inside a scrollview – lucidbrot Apr 03 '21 at 12:36

1 Answers1

1

Looks like problem is in recyclerView.setAdapter(adapter), so:

  1. Check if it is actually calls (it can be skipped because of catch block) - put breakpoint or add some log to check it.
  2. Check is this method calls from MainThread (you call it in callback response).
Yegorf
  • 400
  • 2
  • 11
  • 1
    To add to this answer. When inside of an asynchronous callback, `setAdapter()` may be called after Android has already done its layout pass. It's most reliable to call `setAdapter()` from directly within `onCreate()`. – Sammy T Apr 03 '21 at 18:26