简体   繁体   English

在回收站视图中未连接适配器

[英]No adapter attached in Recycler View

I am new to Android programming and I'm trying to create a cardview using recycler view using an API but I am getting the error saying "E/RecyclerView: No adapter attached; skipping layout". 我是Android编程的新手,我正在尝试使用API​​使用回收站视图创建cardview,但出现错误消息“ E / RecyclerView:未连接适配器;跳过布局”。 I tried looking up the solution online but couldn't seem to figure it out. 我尝试在线查找解决方案,但似乎无法解决。

I tried the solution given by this link : 我尝试了此链接给出的解决方案:

http://www.chansek.com/RecyclerView-no-adapter-attached-skipping-layout/ http://www.chansek.com/RecyclerView-no-adapter-attached-skipping-layout/

public class MainActivity extends AppCompatActivity { 公共类MainActivity扩展了AppCompatActivity {

private RecyclerView mRecyclerView;
private ExampleAdapter mExampleAdapter;
private ArrayList<ExampleItem> mExampleList;
private RequestQueue mRequestQueue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRecyclerView = findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.setHasFixedSize(true);

    mExampleList = new ArrayList<>();
    mRequestQueue = Volley.newRequestQueue(this);
    parseJSON();
}

private void parseJSON()
{
    String url = "http://apidev.travelhouse.world/api/v1/packages";
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new com.android.volley.Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray = response.getJSONArray("");

                for (int i = 0; i < jsonArray.length(); i++)
                {
                    JSONObject hit = jsonArray.getJSONObject(i);

                    String holiday_name = hit.getString("holiday_name");
                    String holiday_price = hit.getString("package_price");
                    String primary_image = hit.getString("primary_image");

                    mExampleList.add(new ExampleItem(primary_image,holiday_name,holiday_price));

                }

                mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
                mRecyclerView.setAdapter(mExampleAdapter);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }


    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();

        }
    })

    {
        /** Passing some request headers* */
        @Override
        public Map getHeaders() throws AuthFailureError {
            HashMap headers = new HashMap();
            headers.put("Content-Type", "application/json");
            headers.put("X-API-KEY", "CODEX@123");
            return headers;
        }
    };

    mRequestQueue.add(request);
}

} }

I think this is solution which Vlad Kramarenko recommended. 我认为这是弗拉德·克拉马连科(Vlad Kramarenko)建议的解决方案。 I just implemented it in your code and I agree with it. 我刚刚在您的代码中实现了它,并且我同意。 So the problem is that you need to set Adapter for RecyclerView at the beginning. 因此,问题在于您需要在开始时设置Adapter for RecyclerView。 Once it is set up, you linked your List of ExampleItems to your RecyclerView through ExampleAdapter. 设置完成后,您可以通过ExampleAdapter将ExampleItems列表链接到RecyclerView。 Now every time you change your list and you want to updated your RecyclerView, you need to call notifyDataSetChanged() method on your Adapter which is linked to RecyclerView. 现在,每次更改列表并更新RecyclerView时,都需要在与RecyclerView链接的适配器上调用notifyDataSetChanged()方法。

private RecyclerView mRecyclerView;
private ExampleAdapter mExampleAdapter;
private ArrayList<ExampleItem> mExampleList;
private RequestQueue mRequestQueue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRecyclerView = findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.setHasFixedSize(true);

    mExampleList = new ArrayList<>();
    mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
    mRecyclerView.setAdapter(mExampleAdapter);

    mRequestQueue = Volley.newRequestQueue(this);
    parseJSON();
}

private void parseJSON()
{
    String url = "http://apidev.travelhouse.world/api/v1/packages";
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new com.android.volley.Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray = response.getJSONArray("");

                for (int i = 0; i < jsonArray.length(); i++)
                {
                    JSONObject hit = jsonArray.getJSONObject(i);

                    String holiday_name = hit.getString("holiday_name");
                    String holiday_price = hit.getString("package_price");
                    String primary_image = hit.getString("primary_image");

                    mExampleList.add(new ExampleItem(primary_image,holiday_name,holiday_price));

                }

                mRecyclerView.getAdapter().notifyDataSetChanged();

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }


    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();

        }
    })

    {
        /** Passing some request headers* */
        @Override
        public Map getHeaders() throws AuthFailureError {
            HashMap headers = new HashMap();
            headers.put("Content-Type", "application/json");
            headers.put("X-API-KEY", "CODEX@123");
            return headers;
        }
    };

    mRequestQueue.add(request);
}

Maybe this will sound strange for you, but it says that you create your recyclerview but forgot to add the adapter to recyclerView... But don't forget, you do it. 也许这对您来说听起来很奇怪,但是它说您创建了recyclerview,但是却忘记了将适配器添加到recyclerView ...但是不要忘记,您要做的。 So why? 所以为什么?

Answer: when the system tries to draw recyclerView it doesn't have an adapter . 答: 当系统尝试绘制recyclerView时,它没有适配器 You attach adapter in parseJSON function after getting a response from the server. 从服务器获得响应后,将适配器附加在parseJSON函数中。 Request and respons from server take some time, but int the meantime system finishes drawing views and says: Hey, your recyclerView doesn't have an adapter, please fix it. 来自服务器的请求和响应需要一些时间,但同时系统会完成绘制视图并说:嘿,您的recyclerView没有适配器,请修复它。 You can ignore this warning, but if you want to make all things perfect: you can attach empty adapter in onCreate, and after getting a response do 您可以忽略此警告,但是如果您想使所有事情都变得完美:您可以在onCreate中附加空适配器,并在获得响应后执行

mRecyclerView.getAdapter().notifyDataSetChanged();

EDIT: this how will look your onCreate: 编辑:这将如何看起来您的onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRecyclerView = findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);

    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.setHasFixedSize(true);

    mExampleList = new ArrayList<>();
    mRecyclerView.setAdapter(new ExampleAdapter(mExampleList, some arguments)

    mRequestQueue = Volley.newRequestQueue(this);
    parseJSON();
}

In the onCreate Method, add after creating array list 在onCreate方法中,创建数组后添加列表

mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);

Remove them from the try clause 从try子句中删除它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 回收站视图:未连接适配器; 跳过布局(API 的回收站视图中的错误) - Recycler view: no adapter attached; skipping layout(Error in recycler view for API ) 未连接适配器; 跳过布局,同时使用回收器视图显示列表 - No adapter attached; skipping layout ,while displaying a list using recycler VIew 为什么回收站视图未显示任何带有错误的帖子:E / RecyclerView:未连接适配器; 跳过布局 - Why is recycler view not showing any posts with error: E/RecyclerView: No adapter attached; skipping layout 图像未附加到回收站视图 - Image not getting attached to recycler view 圆形水平回收器视图适配器 - Circular horizontal Recycler View Adapter Firebase:回收站视图未附加 Adater,跳过布局 - Firebase:Recycler view No Adater attached , Skipping Layout Onclick按钮在适配器中不起作用(Recycler View应用) - Onclick button is not working in adapter (Recycler view app) 回收器视图适配器现在在片段中显示数据 - Recycler view adapter now displaying data in fragment Firebase Recycler View适配器(从Firebase检索) - Firebase Recycler View Adapter (Retrieving from firebase) 如何在构造函数中传递 Recycler 视图适配器? - How to pass a Recycler view adapter in a constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM