没有适配器;跳过布局,但已设置适配器

时间:2016-07-31 16:17:45

标签: android android-recyclerview

@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>

在列表中查看数据,但给出了logcat

No adapter attached; skipping layout

1 个答案:

答案 0 :(得分:0)

我假设在OkHttp请求返回之前显示了这些消息。

在onCreate中尝试此操作。除非您确实需要显示不同类型的数据,否则不应重新分配适配器。

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

在okhttp响应中,您需要执行此操作以使用新数据刷新适配器

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