skip to Main Content

This is the recycler view activity. Here I have initialized the recycler view. Despite that I am getting error. Where might have I gone wrong. In the log cat I get the error

No adapter attached; skipping layout.

Here I am getting response display the response in the recycler view using APIs. I am also using retrofit too. In the get data() function I am taking the response status and getting data.

public class MainActivity extends AppCompatActivity {



    RecyclerView recyclerView;
    ListAdapter1 listAdapter;
//    List<SupermarketModels> supermarketModelsList = new ArrayList<>();
    ApiInterface apiInterface;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initialization();
        getdata();
    }

    private void initialization(){
        recyclerView = findViewById(R.id.recyclerview);
        Retrofit retrofit = APIClient.getclient();
        apiInterface = retrofit.create(ApiInterface.class);
    }

    private void setadapter(List<SupermarketModels> supermarketModels){

        listAdapter = new ListAdapter1(this, supermarketModels);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(linearLayoutManager);

        recyclerView.setAdapter(listAdapter);

        listAdapter.notifyDataSetChanged();


    }
    private void getdata(){
        apiInterface.getList().enqueue(new Callback<GetListResponse>() {
            @Override
            public void onResponse(Call<GetListResponse> call, Response<GetListResponse> response) {

                try {
                    if (response!= null){
                        if (response.body().getStatus().equals("1")){
                            setadapter(response.body().getData());


                        }
                        else {
                            Toast.makeText(MainActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                } catch (Exception e){
                    Log.e("exp", e.getLocalizedMessage());

                }

            }

            @Override
            public void onFailure(Call<GetListResponse> call, Throwable t) {

            }
        });

    }
} 

2

Answers


  1. you have to setOrientation() to your layout manager.and remove notifyDataSetChanged() line from there.

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); //or HORIZONTLE,whatever you want
        
                recyclerView.setLayoutManager(linearLayoutManager);
        
                recyclerView.setAdapter(listAdapter);
        
                listAdapter.notifyDataSetChanged(); //remove this line from here
    
    Login or Signup to reply.
  2. This error can be avoid setting empty adapter first before setting actual adapter, when data is actually loading. In your example before calling getdata() you just call empty adapter like this

    EmptyAdapter emptyAdapter = new EmptyAdapter();
    recyclerView.setAdapter(emptyAdapter);
    

    Here is Empty Adapter class , which you need to create

    import android.view.View;
    import android.view.ViewGroup;
    
    import androidx.recyclerview.widget.RecyclerView;
    
    public class EmptyAdapter extends RecyclerView.Adapter<EmptyAdapter.EmptyHolder> {
    @Override
    public EmptyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }
    
    @Override
    public void onBindViewHolder(EmptyHolder holder, int position) {
    
    }
    
    @Override
    public int getItemCount() {
        return 0;
    }
    
    class EmptyHolder extends RecyclerView.ViewHolder {
        public EmptyHolder(View itemView) {
            super(itemView);
        }
    }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search