gpt4 book ai didi

android - 在 recyclerview 中设置 Adapter

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:19:32 26 4
gpt4 key购买 nike

我正在尝试使用 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 适配器类:-

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

提前致谢

最佳答案

您永远不会初始化适配器 mAppAdapter,因此它在运行时为空。你错过了像 mAppAdapter = new AppAdapter(yourdata); 这样的东西在调用 setAdapter 之前。

关于android - 在 recyclerview 中设置 Adapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36898885/

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