使用带有RecyclerView和Retrofit的片段的麻烦

时间:2017-12-08 05:17:52

标签: android android-layout android-fragments android-recyclerview retrofit2

我有这个错误,实际上我不知道片段。

CreatView

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_inicio, container,
                false);
        getActivity().setTitle(R.string.inicio);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.card_recycler_view);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        loadJSON();
        return rootView;
    }

loadJSON void

private void loadJSON(){
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://192.168.1.42:3000/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    RequestInterface request = retrofit.create(RequestInterface.class);
    Call<JSONResponse> call = request.getJSON();
    call.enqueue(new Callback<JSONResponse>() {
        @Override
        public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
            JSONResponse jsonResponse = response.body();
            data = new ArrayList<>(Arrays.asList(jsonResponse.getCupon()));
            adapter = new CuponAdapter(data);
            recyclerView.setAdapter(adapter);
        }

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

适配器

public class CuponAdapter extends RecyclerView.Adapter<CuponAdapter.ViewHolder> {
private ArrayList<Cupon> cupon;

public CuponAdapter(ArrayList<Cupon> cupon) {
    this.cupon = cupon;
}

@Override
public CuponAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int i) {
    holder.tv_titulo.setText(cupon.get(i).getTitulo());
    holder.tv_empresa.setText(cupon.get(i).getEmpresa());
    holder.tv_descripcion.setText(cupon.get(i).getDescripcion());
}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    private TextView tv_titulo, tv_descripcion, tv_empresa;

    public ViewHolder(View view) {
        super(view);
        tv_titulo = (TextView) view.findViewById(R.id.tv_titulo);
        tv_descripcion = (TextView) view.findViewById(R.id.tv_descripcion);
        tv_empresa = (TextView) view.findViewById(R.id.tv_empresa);
    }
}
}

错误

  

12-08 00:00:05.727 1784-1784 / com.mighty.cupoferta E / RecyclerView:No   适配器;跳过布局

此代码基于this链接。 但他解释了如何投入一项活动,我该如何插入一个片段呢?

3 个答案:

答案 0 :(得分:0)

  call.enqueue(new Callback<JSONResponse>() {
    @Override
    public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
        JSONResponse jsonResponse = response.body();
        data = new ArrayList<>(Arrays.asList(jsonResponse.getCupon()));
        adapter = new CuponAdapter(data);
        recyclerView.setAdapter(adapter);
    }

不要依赖网络响应来设置适配器。

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);

你可以在这里做。

然后,在网络响应后,您可以将数据添加到适配器并调用notifyDataSetChanged()

答案 1 :(得分:0)

好吧,因为您在延迟后设置了adapter,在返回adapter之前尝试将空recyclerview设置为rootView,然后更新adapterRetrofit回调,如

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_inicio, container,
                false);
        getActivity().setTitle(R.string.inicio);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.card_recycler_view);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyvlerView.setAdapter(new CuponAdapter(new ArrayList<Cupon>()));
        loadJSON();
        return rootView;
    }

答案 2 :(得分:0)

在onCreateView函数中设置适配器并在loadJSON函数中使用.notifyDataSetChanged()来通知适配器有关数据中的更改,如下面的代码。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_inicio, container,
            false);
    getActivity().setTitle(R.string.inicio);
    recyclerView = (RecyclerView) rootView.findViewById(R.id.card_recycler_view);
    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);
    loadJSON();
    return rootView;
 }

并在你的适配器类中写下这个

private void loadJSON(){
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://192.168.1.42:3000/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();
RequestInterface request = retrofit.create(RequestInterface.class);
Call<JSONResponse> call = request.getJSON();
call.enqueue(new Callback<JSONResponse>() {
    @Override
    public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
        JSONResponse jsonResponse = response.body();
        data = new ArrayList<>(Arrays.asList(jsonResponse.getCupon()));
        adapter = new CuponAdapter(data);
        adapter.notifyDataSetChanged();
    }

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