从活动接收意图后如何创建片段?

Posted

技术标签:

【中文标题】从活动接收意图后如何创建片段?【英文标题】:How to create fragment after receiving intent from activity? 【发布时间】:2019-10-05 10:09:24 【问题描述】:

我在活动的 onCreate 方法中收到一个胆固醇监测器的数组列表。我试图将它传递到 onPostExecute 的片段中,但目前它传递的是一个空列表? 在我的 onCreate 中。

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

在我的 AsyncTask 方法中

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

编辑:在我的监视器列表片段中

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


    

【问题讨论】:

分享你的 MonitorListFragment 代码 已更新监控列表片段代码 那么,这是this.getIntent().getParcelableArrayListExtra("monitorList");,返回null吗? 确保您使用正确的密钥从意图中获取值并从 getArgumemts().getParcelableArrayList("key") 访问值。如果你采取同样的步骤,请告诉我 @PPartisan 不,它按预期返回胆固醇监视器的数组列表。但是当我将它放入片段时,我得到一个空指针异常。我以为是因为我在 onCreate 中创建了片段,所以它可能会在我获得数组列表之前创建片段。所以我使用了 onPostexecute 但似乎不起作用 【参考方案1】:

如果你先把数据传给fragment会怎么样

    Bundle bundle = new Bundle();
    bundle.putStringArrayList(key,value);
    fragment.setArguments(bundle);

然后在片段中检索

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

【讨论】:

如何在创建的片段中检索? 首先创建CholesterolFragment extends Fragment,然后创建扩展AsyncTask&lt;ArrayList&lt;CholesterolMonitor&gt;, Void, ArrayList&lt;CholesterolMonitor&gt;&gt;的子类(嵌套类)

以上是关于从活动接收意图后如何创建片段?的主要内容,如果未能解决你的问题,请参考以下文章

如何从活动意图到特定片段

使用意图从另一个片段调用一个片段

如何从活动意图到特定片段?

将多个项目添加到列表视图,每个项目从按钮单击的另一个意图接收

活动重新创建意图附加内容为空

如何在创建后通过更改活动来处理推送通知意图?