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
use this code :
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.
make your MainActivity to implement from MyCallBack
create a field like this in your activity
and in your onCreate of your main activity initialize it
in your onResponse do this:
on onGetResponse method update your ui: