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
you have to
setOrientation()
to your layout manager.and removenotifyDataSetChanged()
line from there.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 thisHere is Empty Adapter class , which you need to create