在recyclerview中设置适配器

时间:2016-04-27 19:12:47

标签: android android-fragments android-recyclerview

我正在尝试使用recyclerview并设置适配器,但我收到错误: -

没有连接适配器;跳过布局 所以请帮我解决这个问题

public class FragmentTwo extends Fragment {
private AppAdapter mAppAdapter;
private ArrayList<Model> mModel;
@Bind(R.id.recycler_view)
RecyclerView recyclerView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_two, container, false);
    ButterKnife.bind(this, v);
    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(mAppAdapter);
    parseData();
    return v;
}

 private void parseData() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("url")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    ReqInterface reqInterface = retrofit.create(ReqInterface.class);
    Call<Model> call = reqInterface.getAdvanceData("france");
    call.enqueue(new Callback<Model>() {
        @Override
        public void onResponse(Call<Model> call, Response<Model> response) {
            Model model = response.body();
            mModel = new ArrayList<Model>(Arrays.asList(model));
            mAppAdapter = new AppAdapter(mModel);
            recyclerView.setAdapter(mAppAdapter);
            mAppAdapter.notifyDataSetChanged();
        }
        @Override
        public void onFailure(Call<Model> call, Throwable t) {
        }
     });
    }
  }

这是我的Recyclerview Adapter类: -

public class AppAdapter extends RecyclerView.Adapter<AppAdapter.ViewHolder> {
private ArrayList<Model> mModel;
TextView highTemp;

public AppAdapter(ArrayList<Model> model) {
    mModel = model;
}

public class ViewHolder extends RecyclerView.ViewHolder {
    TextView highTemp;
    public ViewHolder(View view) {
        super(view);
        highTemp = (TextView) view.findViewById(R.id.card_view_tv_hightemp);
    }
}

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

}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    viewHolder.highTemp.setText(String.valueOf(mModel.getName);
}

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

提前致谢

5 个答案:

答案 0 :(得分:3)

您永远不会初始化适配器mAppAdapter,因此它在运行时为空。您错过了mAppAdapter = new AppAdapter(yourdata);之类的内容 在致电setAdapter之前。

答案 1 :(得分:2)

您的适配器mAppAdapter从未初始化,即它为空。

因此recyclerView.setAdapter(mAppAdapter);recyclerView.setAdapter(null);相同,因此您实际上并未设置适配器!

请记住初始化您的字段:

mAppAdapter = new AppAdapter(mModel); // or whatever constructor you want
recyclerView.setAdapter(mAppAdapter);

答案 2 :(得分:1)

更改

recyclerView.setAdapter(mAppAdapter);

recyclerView.setAdapter(new AppAdapter(model));

答案 3 :(得分:1)

您永远不会创建mAppAdapter

要解决此问题,请创建适配器的新实例,并将其传递给.setAdapter的参数。

换句话说,您应该使用.setAdapter(new AppAdapter(...)),其中...替换为您在AppAdapter中创建的参数。

答案 4 :(得分:1)

我通常在asynctask中进行改装调用,然后当您从服务器获得响应时,您创建一个新适配器并将其设置为recyclerview。 无论如何,尝试从onCreate()方法中删除.setAdapter行,然后保留它。 它起作用并且它没有崩溃,因为它崩溃了,因为适配器为空(它没有元素)。

我希望它有所帮助!