我是Android编程的新手,我正在尝试使用API使用回收站视图来创建cardview,但出现错误消息“ E / RecyclerView:未连接适配器;跳过布局”。我尝试在线查找解决方案,但似乎无法解决。
我尝试了此链接提供的解决方案:
http://www.chansek.com/RecyclerView-no-adapter-attached-skipping-layout/
公共类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);
}
}
答案 0 :(得分:1)
我认为这是弗拉德·克拉马连科(Vlad Kramarenko)推荐的解决方案。我刚刚在您的代码中实现了它,并且我同意。 因此,问题在于您需要在开始时设置Adapter for RecyclerView。设置完成后,您可以通过ExampleAdapter将ExampleItems列表链接到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);
}
答案 1 :(得分:0)
也许这对您来说听起来很奇怪,但是它说您创建了recyclerview,但是却忘记了将适配器添加到recyclerView ...但是请不要忘记,您要做的。所以为什么?
回答:系统尝试绘制recyclerView时,它没有适配器。从服务器获得响应后,将适配器附加在parseJSON函数中。来自服务器的请求和响应需要一些时间,但同时系统会完成绘制视图并说:嘿,您的recyclerView没有适配器,请修复它。您可以忽略此警告,但是如果您想使所有事情都变得完美:您可以将空适配器附加到onCreate中,并在获得响应后执行
mRecyclerView.getAdapter().notifyDataSetChanged();
编辑: 这样您的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();
}
答案 2 :(得分:0)
在onCreate方法中,在创建数组列表后添加
mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);
从try子句中删除它们