gpt4 book ai didi

java - 回收者 View 中未连接适配器

转载 作者:行者123 更新时间:2023-12-02 09:40:49 25 4
gpt4 key购买 nike

我是 Android 编程新手,我正在尝试使用 API 使用回收器 View 创建卡片 View ,但收到错误消息“E/RecyclerView:没有附加适配器;跳过布局”。我尝试在网上查找解决方案,但似乎无法弄清楚。

我尝试了此链接给出的解决方案:

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

公共(public)类 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);
}

}

最佳答案

我认为这是 Vlad Kramarenko 推荐的解决方案。我刚刚在你的代码中实现了它,我同意它。所以问题是一开始就需要为RecyclerView设置Adapter。设置完成后,您可以通过ExampleAdapter 将ExampleItems 列表链接到RecyclerView。现在,每次更改列表并且想要更新 RecyclerView 时,您都需要在链接到 RecyclerView 的 Adapter 上调用 notificationDataSetChanged() 方法。

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);
}

关于java - 回收者 View 中未连接适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57082831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com