public class RecyclerFragment extends Fragment{
private View view;
private ArrayList<ForumEntity> forumEntities =new ArrayList<>();
private NewmyAdapter newmyAdapter;
private Gson mGson=new Gson();
public ArrayList<ForumEntity> getForumEntities() {
return forumEntities;
}
public void setForumEntities(ArrayList<ForumEntity> forumEntities) {
this.forumEntities = forumEntities;
newmyAdapter=new NewmyAdapter(forumEntities,getActivity());//进入适配器Adapter,转换数据
RecyclerView recyclerView=view.findViewById(R.id.rv);
recyclerView.setAdapter(newmyAdapter);
}
@NonNull
@Override
public View onCreateView(@NonNull LayoutInflater inflater,@NonNull ViewGroup container,@NonNull Bundle saveInstanceState){
view = inflater.inflate(R.layout.fragment_recycler,container,false);
initdata();
List<ForumEntity>temp=forumEntities;
RecyclerView recyclerView=view.findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
newmyAdapter=new NewmyAdapter(forumEntities,getActivity());//进入适配器Adapter,转换数据
recyclerView.setAdapter(newmyAdapter);
newmyAdapter.setRecyclerItemClickListener(new NewmyAdapter.OnRecyclerItemClickListener() {
@Override
public void onRecyclerItemclick(int position) {
System.out.println(forumEntities.get(position).getFo_id());
}
});
return view;
}
private void initdata(){
try {
OkHttpUtils.getInstance().doGet("http://192.168.161.1:8081/selectForum", new MyCallBack() {
@Override
public void onSuccess(String result) {
Type type=new TypeToken<List<ForumEntity>>(){}.getType();
forumEntities= mGson.fromJson(result,type);
if(forumEntities.isEmpty()){
System.out.println("wowowowowowowoooooooooooooo");
}else{
System.out.println("yoyouyouyouyouyouyouyoyou");
}
}
@Override
public void onError(Exception e) {
Toast.makeText(getActivity(),"Error",Toast.LENGTH_SHORT).show();
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
}
为什么第一次拿到的forumentities是null的呢?第二次才不为空。?