Android 使用改装和碎片回收视图(未连接适配器;跳过布局)

Android 使用改装和碎片回收视图(未连接适配器;跳过布局),android,android-studio,android-fragments,android-recyclerview,retrofit2,Android,Android Studio,Android Fragments,Android Recyclerview,Retrofit2,嗨,我试图显示一个来自改装onResponse的列表,我正在片段中使用recyclerview。列表应该显示厨师,但我从RecyclerView得到一个错误 连接类 public class Conexion { public static String BASE_URL = "http://10.30.0.133:8091/Service1.asmx/"; private static Retrofit retrofit = null; public static

嗨,我试图显示一个来自改装onResponse的列表,我正在片段中使用recyclerview。列表应该显示厨师,但我从RecyclerView得到一个错误

连接类

public class Conexion {


    public static String BASE_URL = "http://10.30.0.133:8091/Service1.asmx/";
    private static Retrofit retrofit = null;

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


}
端点接口

public interface EndPointsInterface {


    @GET("chef/{idUser}")
    Call<ChefResponse> GetChefsCercanos(@Path("idUser") Integer id_usuario);


}
实体厨师

public class Chef extends Usuario{

    @SerializedName("id_chef")
    private Integer id_chef;
    @SerializedName("TipoServicio")
    private String TipoServicio;
    @SerializedName("Rating")
    private Double Rating;
    @SerializedName("EstadoChef")
    private Boolean EstadoChef;


    public Chef(){}

    public Chef(Integer id_usuario,String NombreUsuario,String ApellidoUsuario,String TelefonoUsuario,String Email,String Contraseña,Double pos_x,Double pos_y,Integer id_chef,String TipoServicio,Double Rating,Boolean EstadoChef){
        super(id_usuario,NombreUsuario,ApellidoUsuario,TelefonoUsuario,Email,Contraseña,pos_x,pos_y);
        this.id_chef=id_chef;
        this.TipoServicio=TipoServicio;
        this.Rating=Rating;
        this.EstadoChef=EstadoChef;
    }



    public Integer getId_chef() {
        return id_chef;
    }

    public void setId_chef(Integer id_chef) {
        this.id_chef = id_chef;
    }

    public String getTipoServicio() {
        return TipoServicio;
    }

    public void setTipoServicio(String tipoServicio) {
        TipoServicio = tipoServicio;
    }

    public Double getRating() {
        return Rating;
    }

    public void setRating(Double rating) {
        Rating = rating;
    }

    public Boolean getEstadoChef() {
        return EstadoChef;
    }

    public void setEstadoChef(Boolean estadoChef) {
        EstadoChef = estadoChef;
    }
}
厨师长回应

public class ChefResponse {


    @SerializedName("results")
    private Chef[] results;


    public Chef[] getresults(){
        return results;
    }
}
回收视图适配器

public class ListaChefsCercanos extends RecyclerView.Adapter<ListaChefsCercanos.ListaChefsCercanosViewHolder> {


    private List<Chef> chefs;
    private List<Usuario> usuarios;
    private int rowLayout;
    private Context context;


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

    @Override
    public void onBindViewHolder(ListaChefsCercanosViewHolder holder, int position) {
        holder.nombreschefscerca.setText(chefs.get(position).getNombreUsuario());
        holder.ratingchef.setText(chefs.get(position).getRating().toString());
    }

    @Override
    public int getItemCount() {

        return chefs.size();
    }

    public static class ListaChefsCercanosViewHolder extends RecyclerView.ViewHolder{
        LinearLayout chefslayout;
        TextView nombreschefscerca;
        TextView ratingchef;

        public ListaChefsCercanosViewHolder(View v){
            super(v);
            chefslayout=(LinearLayout) v.findViewById(R.id.cheflayoutcerca);
            nombreschefscerca=(TextView) v.findViewById(R.id.tv_NombreChefCercano);
            ratingchef=(TextView) v.findViewById(R.id.tv_RatingChefCercano);
        }
    }

    public ListaChefsCercanos(ArrayList <Chef> chefs){
        this.chefs=chefs;
        //this.rowLayout = rowLayout;
        //this.context = context;
    }



}

更新:在OncreateView()中为RecyclerView使用空适配器


更新:在OncreateView()中为RecyclerView使用空适配器


可能重复:我按照Diogo Rosa的建议得到了错误,但仍然没有在recyclerview中显示列表。。有什么建议吗?当我调试时发现:E/RecyclerView:没有连接适配器;跳过布局在OnCreateView中的第一个setAdapter中,您必须首先向适配器添加一个列表,即使该列表为空。。在适配器declare chef List中尝试获取空列表的大小时,应用程序正在崩溃,如以下私有列表chefs=new ArrayList();可能重复:我按照Diogo Rosa的建议得到了错误,但仍然没有在recyclerview中显示列表。。有什么建议吗?当我调试时发现:E/RecyclerView:没有连接适配器;跳过布局在OnCreateView中的第一个setAdapter中,您必须首先向适配器添加一个列表,即使该列表为空。。在适配器declare chef List中尝试获取空列表的大小时,应用程序正在崩溃,如以下私有列表chefs=new ArrayList();我已经粘贴了我片段的onCreateView,但它仍然没有显示任何内容,我得到了相同的错误:E/RecyclerView:没有连接适配器;跳过布局当我从onResponse向外部粘贴setApapter,但在onCreateView内部粘贴setApapter时,我遇到了其他错误,应用程序掉了。错误为:recyclerView.setAdapter(new YourAdapter(getCurrentActivity());我应该把setAdapter放在onResponse的内部还是外部???两者都有,但我只是写了“YourAdapter”作为一个例子,您需要使用新的ListaChefsCercanos(上下文)。在onCreateView中设置这个空适配器,然后在Response上覆盖它我已经在这两个版本中设置了适配器,我得到了一个新的错误:java.lang.NullPointerException,最后一个版本是getItemCount(ListCacheFScercanos.java:52)感谢您的帮助,您能远程帮助我使用teamviewer吗?我已经在片段的onCreateView中粘贴了,但它仍然没有显示任何内容,我得到了相同的错误:E/RecyclerView:没有连接适配器;跳过布局当我从onResponse向外部粘贴setApapter,但在onCreateView内部粘贴setApapter时,我遇到了其他错误,应用程序掉了。错误为:recyclerView.setAdapter(new YourAdapter(getCurrentActivity());我应该把setAdapter放在onResponse的内部还是外部???两者都有,但我只是写了“YourAdapter”作为一个例子,您需要使用新的ListaChefsCercanos(上下文)。在onCreateView中设置这个空适配器,然后在Response中覆盖它我已经在这两个版本中设置了适配器,我得到了这个新错误:java.lang.NullPointerException,最后一个版本是getItemCount(ListCacheFScercanos.java:52)。谢谢你的帮助。你能远程帮助我使用teamviewer吗?
public class ListaChefsCercanos extends RecyclerView.Adapter<ListaChefsCercanos.ListaChefsCercanosViewHolder> {


    private List<Chef> chefs;
    private List<Usuario> usuarios;
    private int rowLayout;
    private Context context;


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

    @Override
    public void onBindViewHolder(ListaChefsCercanosViewHolder holder, int position) {
        holder.nombreschefscerca.setText(chefs.get(position).getNombreUsuario());
        holder.ratingchef.setText(chefs.get(position).getRating().toString());
    }

    @Override
    public int getItemCount() {

        return chefs.size();
    }

    public static class ListaChefsCercanosViewHolder extends RecyclerView.ViewHolder{
        LinearLayout chefslayout;
        TextView nombreschefscerca;
        TextView ratingchef;

        public ListaChefsCercanosViewHolder(View v){
            super(v);
            chefslayout=(LinearLayout) v.findViewById(R.id.cheflayoutcerca);
            nombreschefscerca=(TextView) v.findViewById(R.id.tv_NombreChefCercano);
            ratingchef=(TextView) v.findViewById(R.id.tv_RatingChefCercano);
        }
    }

    public ListaChefsCercanos(ArrayList <Chef> chefs){
        this.chefs=chefs;
        //this.rowLayout = rowLayout;
        //this.context = context;
    }



}
public class RecomendadosFragment extends Fragment {

    private RatingBar ratingBar;

    //ListAdapter adapter;
    ArrayList<Chef> listachef;
    ListView lvLista;
    String tag_json_array="jarray req";
    RecyclerView recyclerviewChefsCarnos;
    ListaChefsCercanos mListaChefsCercanos;

    private ArrayList<Chef> data;
    private ListaChefsCercanos adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View x =  inflater.inflate(R.layout.recomendados,null);


        //ratingBar = (RatingBar) x.findViewById(R.id.rb_RatingChefCercano);
        recyclerviewChefsCarnos=(RecyclerView) x.findViewById(R.id.rv_chefsCernaos);
        recyclerviewChefsCarnos.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));


        EndPointsInterface apiService = Conexion.getClient().create(EndPointsInterface.class);


        Call<ChefResponse> call = apiService.GetChefsCercanos(5);
        call.enqueue(new Callback<ChefResponse>() {
            @Override
            public void onResponse(Call<ChefResponse> call, Response<ChefResponse> response) {
                int statusCode = response.code();

                  if (response.isSuccessful()) {

                ChefResponse jsonResponse = response.body();

                if(jsonResponse != null) {

                    data = new ArrayList<>(Arrays.asList(jsonResponse.getresults()));
                    adapter= new ListaChefsCercanos(data);
                    recyclerviewChefsCarnos.setAdapter(adapter);

                }

            } else {
                // Do whatever you want if API is unsuccessful.
            }


               // List<Chef> chefs= response.body().getResults();
               // recyclerviewChefsCarnos.setAdapter(new ListaChefsCercanos( chefs,R.layout.itemchefscercanos,getActivity().getApplicationContext()));
            }

            @Override
            public void onFailure(Call<ChefResponse> call, Throwable t) {
                Log.d("Error",t.getMessage());

            }


        });

        return x;
    }
 E/RecyclerView: No adapter attached; skipping layout
recyclerView.setAdapter(new YourAdapter(getCurrentActivity()));