Fragment中的RecyclerView - 如何从数组列表中检索数据

时间:2018-02-12 19:06:00

标签: java android android-fragments android-recyclerview android-navigation-drawer

所以我看到这个tutorial在导航抽屉的碎片中创建了一个recyclerview。在本教程中,数据存储在一个数组中。我现在要做的是将数据存储在ArrayList中。

这是我在'Tutor'片段中的代码:

   public class Tutors extends Fragment
{
    View myView;
    private ArrayList<TutorModel> tutorModelList = new ArrayList<>();
    private RecyclerView recyclerView;
    private MyAdapter tutorAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getTutorData();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.admin_manage_tutors,
                container, false);
        recyclerView = myView.findViewById(R.id.recycler_view);

        tutorAdapter = new MyAdapter(tutorModelList);
        RecyclerView.LayoutManager mLayoutManager = new
                LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(tutorAdapter);
        return myView;
    }


    public void getTutorData()
    {
        TutorModel tutor = new TutorModel("Juan", "Computer Programming");
        tutorModelList.add(tutor);

        tutor = new TutorModel("Santiago", "French History");
        tutorModelList.add(tutor);

        tutor = new TutorModel("Jose", "Philippine History");
        tutorModelList.add(tutor);

        tutorAdapter.notifyDataSetChanged();

    }
}

这是我的适配器的代码:

public class MyAdapter extends
        RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    public List<TutorModel> tutorList;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView name, subjects;

        public MyViewHolder(View view) {
            super(view);
            name = view.findViewById(R.id.name_tutor);
            subjects = view.findViewById(R.id.subjects_tutor);
        }
    }


    public MyAdapter(List<TutorModel> tutorList) {
        this.tutorList = tutorList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.tutor_list, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        TutorModel tutor = tutorList.get(position);
        holder.name.setText(tutor.getTutorName());
        holder.subjects.setText(tutor.getTutorSubject());
    }

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

这是我的getter / setter代码:

public class TutorModel
{
    String tutorName, tutorSubject;

    public String getTutorName() {
        return tutorName;
    }

    public void setTutorName(String tutorName) {
        this.tutorName = tutorName;
    }

    public String getTutorSubject() {
        return tutorSubject;
    }

    public void setTutorSubject(String tutorSubject) {
        this.tutorSubject = tutorSubject;
    }

    public TutorModel(String tutorName, String tutorSubject) {
        this.tutorName = tutorName;
        this.tutorSubject = tutorSubject;
    }

    public TutorModel() {
    }
}

当我尝试运行该应用时,它会崩溃。

2 个答案:

答案 0 :(得分:1)

onCreateView之后将调用

onCreate,但getTutorData();通知onCreateView尚未初始化的适配器,因此在getTutorData();方法之前初始化您的适配器调用

或更好的方法是在getTutorData

中调用onCreateView
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.admin_manage_tutors,
            container, false);
    recyclerView = myView.findViewById(R.id.recycler_view);

    tutorAdapter = new MyAdapter(tutorModelList);
    RecyclerView.LayoutManager mLayoutManager = new
            LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(tutorAdapter);
    getTutorData();
    return myView;
}

答案 1 :(得分:0)

您已在 onCreateView()中初始化了tutorAdapter,但在 onCreateView 之前调用了onCreate,因此您的适配器为null ..只需编辑您的onCreate,如下面的代码.. < / p>

@Override
   public void onCreate(@Nullable Bundle         savedInstanceState)  
     {  super.onCreate(savedInstanceState);
         tutorAdapter = new MyAdapter(tutorModelList);
        getTutorData(); }