wkchen wkchen - 4 months ago 38
Android Question

No adapter attached; skipping layout, but have set the adapter

@Override
public void onResponse(String response, int id) {
if (response != null) {
try {
JSONObject all = new JSONObject(response);
List<cityBean.CityInfoBean> city = new Gson().fromJson(all.getString("city_info"), new TypeToken<ArrayList<cityBean.CityInfoBean>>() {
}.getType());
for (int i = 0; i < city.size(); i++) {
mData.add(city.get(i).getCity());
}
adapter = new MyAdapter(mData,MainActivity.this, R.layout.recyclerview_item);
mRv_main.setAdapter(adapter);
mSwipeRefreshLayout.setRefreshing(false);
} catch (JSONException e) {
e.printStackTrace();
}
}





E/RecyclerView: No adapter attached; skipping layout
E/RecyclerView: No adapter attached; skipping layout





<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="80dp"
app:cardCornerRadius="10dp"
app:cardElevation="5dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<TextView
android:id="@+id/tv_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</android.support.v7.widget.CardView>


In the list to see the data, but given logcat

No adapter attached; skipping layout

Answer

I'm assuming those messages are shown before the OkHttp request returns.

Try this in onCreate. You shouldn't be reassigning the adapter unless you actually need to display a different type of data.

adapter = new MyAdapter(mData,MainActivity.this, R.layout.recyclerview_item);
mRv_main.setAdapter(adapter);

In the okhttp response, you need to do this to refresh the adapter with new data

mData.clear();
for (int i = 0; i < city.size(); i++) {
    mData.add(city.get(i).getCity());
}
adapter.notifyDataSetChanged();
mSwipeRefreshLayout.setRefreshing(false);
Comments