改造不良请求和RecyclerView:没有连接适配器;跳过布局

时间:2018-05-09 08:15:16

标签: java android android-recyclerview retrofit2

您好我不确定为什么我的回收站视图没有显示,但即​​使在E/RecyclerView: No adapter attached; skipping layout 方法初始化后,我也会遇到以下错误,正如其他遇到相同问题的SO用户所提到的那样:

onResponse

也许Retrofit无法获取数据,因此永远不会调用onFailure因此调用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; }} 。我正在使用在线JSON API。链接: https://reqres.in/api/users?page=1&per_page=3

ApiClient.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;
}
}

DataResponse.java

public interface ApiInterface {

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

ApiInterface

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

RecyclerAdapter.java

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

MainActivity

war

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

2 个答案:

答案 0 :(得分:1)

您正在制作array request,将其转换为object requestyour api endpoint is returning json object)。像这样改变呼叫

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接口应该是这样的

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

答案 1 :(得分:0)

它的发生是因为您的请求失败而且您没有为回收站视图设置适配器更改它

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;`
private ProgressBar progressBar;
private GridLayoutManager layoutManager;
private RecyclerAdapter adapter;
private ApiInterface apiInterface;
@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);
adapter = new RecyclerAdapter(data, MainActivity.this);
        recyclerView.setAdapter(adapter);

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.notifydatasetchanged()
        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();
    }
});
}