【发布时间】: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 但似乎不起作用
标签: android android-fragments android-intent android-asynctask