skip to Main Content

i got error message : recyclerview No adapter attached; skipping layout
when i try to fetch data and store to recyclerview

here is my code:

public class MainActivity extends AppCompatActivity {

private DataAdapter dataAdapter;
private ArrayList<OrderListArray> orderListArrays=new ArrayList<>();
private RecyclerView mRecyclerView;
private ProgressBar mProgressBar;


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

    getData();

    mProgressBar=(ProgressBar)findViewById(R.id.progress_bar);
    mProgressBar.setVisibility(View.VISIBLE);
    mRecyclerView=(RecyclerView)findViewById(R.id.order_list);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}

public void getData(){
    String userToken = Preferences.getUserToken(getBaseContext());
    Log.d("[DEBUG]", "userToken: " + userToken);
    Call<List<OrderListArray>> getDataCall = ApiClient.getUserService().getJson("Bearer " + userToken);

    getDataCall.enqueue(new Callback<List<OrderListArray>>() {
        @Override
        public void onResponse(Call<List<OrderListArray>> call, Response<List<OrderListArray>> response) {

            mProgressBar.setVisibility(View.GONE);
            if (response.isSuccessful() && response.body()!=null) {
                orderListArrays = new ArrayList<>(response.body());
                  dataAdapter=new DataAdapter(orderListArrays,MainActivity.this);
                  mRecyclerView.setAdapter(dataAdapter);
            }
        }

        @Override
        public void onFailure(Call<List<OrderListArray>> call, Throwable t) {
              mProgressBar.setVisibility(View.GONE);
            Toast.makeText(MainActivity.this,"Failure",Toast.LENGTH_SHORT).show();
        }
    });
}

}

2

Answers


  1. use this code :

        public class MainActivity extends AppCompatActivity {
        
        private DataAdapter dataAdapter;
        private ArrayList<OrderListArray> orderListArrays=new ArrayList<>();
        private RecyclerView mRecyclerView;
        private ProgressBar mProgressBar;
        
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        
            getData();
        
            mProgressBar=(ProgressBar)findViewById(R.id.progress_bar);
            mProgressBar.setVisibility(View.VISIBLE);
            mRecyclerView=(RecyclerView)findViewById(R.id.order_list);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            //change here
            dataAdapter=new DataAdapter(orderListArrays,this);
            mRecyclerView.setAdapter(dataAdapter);
        }
        
        public void getData(){
            String userToken = Preferences.getUserToken(getBaseContext());
            Log.d("[DEBUG]", "userToken: " + userToken);
            Call<List<OrderListArray>> getDataCall = ApiClient.getUserService().getJson("Bearer " + userToken);
        
            getDataCall.enqueue(new Callback<List<OrderListArray>>() {
                @Override
                public void onResponse(Call<List<OrderListArray>> call, Response<List<OrderListArray>> response) {
        
                    mProgressBar.setVisibility(View.GONE);
                    if (response.isSuccessful() && response.body()!=null) {
                        orderListArrays = new ArrayList<>(response.body());
                        //change here
                        dataAdapter.notifyDataSetChanged()
                    }
                }
        
                @Override
                public void onFailure(Call<List<OrderListArray>> call, Throwable t) {
                      mProgressBar.setVisibility(View.GONE);
                    Toast.makeText(MainActivity.this,"Failure",Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    
    Login or Signup to reply.
  2. I guess it is because you have some operation that must run in the Main Thread, but you write your code in the background thread(onResponse method), to solve this, you can create an interface callback, and invoke your callback method in onResponse and update your UI in the main thread.

    public interface MyCallBack {
        void onGetResponse(Object response);
    }
    

    make your MainActivity to implement from MyCallBack

    public class MainActivity extends AppCompatActivity implements MyCallBack 
    

    create a field like this in your activity

    public class MainActivity extends AppCompatActivity implements MyCallBack {
        private MyCallback myCallback;
    

    and in your onCreate of your main activity initialize it

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myCallback = this;
        setContentView(R.layout.activity_main);
    

    in your onResponse do this:

    public void onResponse(Call<List<OrderListArray>> call, Response<List<OrderListArray>> response) {
        if (response.isSuccessful() && response.body()!=null) {
                myCallback.onGetResponse(response.body());
        }
    }
    

    on onGetResponse method update your ui:

    @Override
    public void onGetResponse(Object response) {
        mProgressBar.setVisibility(View.GONE);
        orderListArrays = new ArrayList<>(response.body());
        dataAdapter=new DataAdapter(orderListArrays,MainActivity.this);
        mRecyclerView.setAdapter(dataAdapter);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search