从活动中收到意图后如何创建碎片



我在活动方法中收到一个胆固醇监测器的数组列表。我正在尝试将其传递到Onpostexecute中的片段中,但是当前它在零列表中传递?在我的on Create中。

cholesterol_monitor= this.getIntent().getParcelableArrayListExtra("monitorList");
        CholesterolFragment cholesterolFragment= new CholesterolFragment();
        cholesterolFragment.execute(cholesterol_monitor);

在我的异步方法中

 private class CholesterolFragment extends AsyncTask<ArrayList<CholesterolMonitor>, Void, ArrayList<CholesterolMonitor>> {

        @Override
        protected ArrayList<CholesterolMonitor> doInBackground(ArrayList<CholesterolMonitor>... arrayLists) {
            cholesterol_monitor=arrayLists[0];
            return cholesterol_monitor;
        }
        @Override
        protected void onPostExecute(ArrayList<CholesterolMonitor> result){
            cholesterol_monitor= result;
            monitorListFragment = MonitorListFragment.newInstance(cholesterol_monitor);
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragment_monitor_layout, monitorListFragment)
                    .commit();
        }
    }

编辑:在我的MonitorListFragment

 public static MonitorListFragment newInstance(ArrayList<CholesterolMonitor> monitor_list) {
        MonitorListFragment fragment = new MonitorListFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList(ARG_MONITOR_LIST, monitor_list);
        fragment.setArguments(args);
        return fragment;
    }

编辑:它是因为我的列表中有空对象,所以我在监视器列表中添加了一个检查回收器适配器,但没有显示名称?

这在我的Monitorlistrecycleradapter

 public void onBindViewHolder(final MonitorListRecyclerAdapter.ViewHolder holder, int position) {
    if (!(data.get(position).getPatient()== null)){
       String patient = "Patient: " + data.get(position).getPatient().getName().get(0).getNameAsSingleString();
       String  cholesterolLevel = "Cholesterol: "
            + String.format("%.2f", data.get(position).getPatientCholesterolLevel()) + " "
            + data.get(position).getUnitsMeasurement();
        holder.patientNameView.setText(patient);
        holder.cholesterolView.setText(cholesterolLevel);

    }

如果首先将数据传递到片段

,那怎么样
    Bundle bundle = new Bundle();
    bundle.putStringArrayList(key,value);
    fragment.setArguments(bundle);

然后在片段中检索

Bundle bundle = this.getArguments();
if (bundle != null) {
  // execute AsyncTask class  
}

最新更新