gpt4 book ai didi

java - 改造不良请求和 RecyclerView : No adapter attached; skipping layout

转载 作者:太空狗 更新时间:2023-10-29 13:48:35 24 4
gpt4 key购买 nike

您好,我不确定为什么我的回收站 View 没有显示,但即​​使在 onCreate 方法上初始化它后,我也会收到以下错误,正如其他遇到同样问题的 SO 用户提到的那样:

E/RecyclerView: No adapter attached; skipping layout

也可能 Retrofit 无法获取数据,因此 onResponse 永远不会被调用,因此 onFailure 被调用。我正在使用在线 JSON API。关联: https://reqres.in/api/users?page=1&per_page=3

ApiClient.java

public class ApiClient {
public static final String BASE_URL = "https://reqres.in/api/";
public static Retrofit retrofit = null;

public static Retrofit getApiClient(){
if(retrofit == null){
retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create()).build();
}
return retrofit;
}}

数据响应.java

public class DataResponse {

@SerializedName("page")
private int page;

@SerializedName("per_page")
private int per_page;

@SerializedName("total")
private int total;

@SerializedName("total_pages")
private int total_pages;

@SerializedName("data")
List<Data> data;

public int getPage() {
return page;
}

public int getPer_page() {
return per_page;
}

public int getTotal() {
return total;
}

public int getTotal_pages() {
return total_pages;
}

public List<Data> getData() {
return data;
}
}



class Data {

@SerializedName("id")
private int id;

@SerializedName("first_name")
private String first_name;

@SerializedName("last_name")
private String last_name;

@SerializedName("avatar")
private String avatar;

public int getId() {
return id;
}

public String getFirst_name() {
return first_name;
}

public String getLast_name() {
return last_name;
}

public String getAvatar() {
return avatar;
}
}

API接口(interface)

public interface ApiInterface {

@GET("users")
Call<List<DataResponse>> getUsers(@Query("page") int page, @Query("per_page") int per_page);}

回收适配器.java

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {

private List<Data> dataList;
private Context context;

public RecyclerAdapter(List<Data> dataList, Context context){

this.dataList = dataList;
this.context = context;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

Data data = dataList.get(position);
holder.fullname.setText(data.getFirst_name() + ", " + data.getLast_name());
Glide.with(context).load(data.getAvatar()).into(holder.avatar);
}

@Override
public int getItemCount() {
return dataList.size();
}

public static class MyViewHolder extends RecyclerView.ViewHolder {

ImageView avatar;
TextView fullname;

public MyViewHolder(View itemView) {
super(itemView);
avatar = (ImageView) itemView.findViewById(R.id.cardView_image);
fullname = (TextView) itemView.findViewById(R.id.cardView_location);
}
}

public void addData(List<Data> data){

for(Data d: data){
dataList.add(d);
}
}}

主要 Activity

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private ProgressBar progressBar;
private GridLayoutManager layoutManager;
private RecyclerAdapter adapter;

private ApiInterface apiInterface;

private int page_num = 1;
private int per_page = 3;

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

adapter = null;

recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
layoutManager = new GridLayoutManager(this, 2);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);

apiInterface = ApiClient.getApiClient().create(ApiInterface.class);

progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);

Call<List<DataResponse>> call = apiInterface.getUsers(page_num, per_page);

call.enqueue(new Callback<List<DataResponse>>() {
@Override
public void onResponse(Call<List<DataResponse>> call, Response<List<DataResponse>> response) {
Toast.makeText(MainActivity.this, "Response success....", Toast.LENGTH_SHORT).show();
List<Data> data = response.body().get(1).getData();
adapter = new RecyclerAdapter(data, MainActivity.this);
recyclerView.setAdapter(adapter);
Toast.makeText(MainActivity.this, "First page is loaded...", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}

@Override
public void onFailure(Call<List<DataResponse>> call, Throwable t) {
Toast.makeText(MainActivity.this, "Bad request", Toast.LENGTH_SHORT).show();
}
});
}}

非常感谢任何帮助,谢谢。

最佳答案

您正在发出一个数组请求,将其转换为对象请求(您的 api 端点正在返回 json 对象)。像这样更改调用

Call<DataResponse> call = apiInterface.getUsers(page_num, per_page);

call.enqueue(new Callback<DataResponse>() {
@Override
public void onResponse(Call<DataResponse> call, Response<DataResponse> response) {
Toast.makeText(MainActivity.this, "Response success....", Toast.LENGTH_SHORT).show();
List<Data> data = response.body().getData();
adapter = new RecyclerAdapter(data, MainActivity.this);
recyclerView.setAdapter(adapter);
Toast.makeText(MainActivity.this, "First page is loaded...", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}

@Override
public void onFailure(Call<DataResponse> call, Throwable t) {
Toast.makeText(MainActivity.this, "Bad request", Toast.LENGTH_SHORT).show();
}
});

api接口(interface)应该是这样的

public interface ApiInterface {
@GET("users")
Call<DataResponse> getUsers(@Query("page") int page,
@Query("per_page") int per_page);
}

关于java - 改造不良请求和 RecyclerView : No adapter attached; skipping layout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50248572/

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