Android 使用Web服务[API]进行表格布局

Android 使用Web服务[API]进行表格布局,android,fragment,android-tablayout,Android,Fragment,Android Tablayout,我试图在TabLayout下的片段中填充一个recyclerview。一切似乎都很好,但数据实际上并未填充到recyclerview中,而所有内容日志[Log.e();]都工作正常。有人能把这个问题联系起来吗 我的片段是这样的: public class SugarLevelReport extends Fragment { private ConstraintLayout view_constraintLayout; private RecyclerView blood_sugar_recyc

我试图在TabLayout下的片段中填充一个recyclerview。一切似乎都很好,但数据实际上并未填充到recyclerview中,而所有内容日志[Log.e();]都工作正常。有人能把这个问题联系起来吗

我的片段是这样的:

public class SugarLevelReport extends Fragment
{
private ConstraintLayout view_constraintLayout;
private RecyclerView blood_sugar_recyclerView;
private ProgressBar progressBar;

private ArrayList<PatientRecordModel> patientRecordModelArrayList = new ArrayList<PatientRecordModel>();
private RecyclerView.Adapter bloodSugaradapter;

// for intent data
public String patient_id_from_intent, patient_name_from_intent, patient_dob_from_intent, patient_mobile_from_intent, patient_email_from_intent;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_sugar_level_report, container, false);

    findViewById(view);
    return view;  
}
public void findViewById(View view)
{
    view_constraintLayout = (ConstraintLayout)view.findViewById(R.id.view_constraintLayout);
    /*progressBar = (ProgressBar)view.findViewById(R.id.progressBar);*/
    blood_sugar_recyclerView = (RecyclerView)view.findViewById(R.id.blood_sugar_recyclerView);

    blood_sugar_recyclerView.setHasFixedSize(true);
    blood_sugar_recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    bloodSugaradapter = new SugarLevelReportAdapter(getActivity(), patientRecordModelArrayList);
    blood_sugar_recyclerView.setAdapter(bloodSugaradapter);

    loadData();
}
public void loadData()
{
    if(GlobalMethods.isNetworkConnected(getActivity()))
    {

        String[] names = getResources().getStringArray(R.array.names);

        for (int i = 0 ; i < names.length ; i++)
        {
            PatientRecordModel patientRecordModel = new PatientRecordModel();
            patientRecordModel.setPatient_name(names[i]);

            // log here is working fine , and its giving correct data
            Log.e("patientsName", patientRecordModel.getPatient_name());

            patientRecordModelArrayList.add(patientRecordModel);
            bloodSugaradapter.notifyDataSetChanged();
        }
    }
    else
    {

    }
}
公共类SugarLevelReport扩展了片段
{
private ConstraintLayout视图\u ConstraintLayout;
私人回收站查看血糖回收站查看;
私人ProgressBar ProgressBar;
private ArrayList patientRecordModelArrayList=新建ArrayList();
私人回收器查看适配器血糖仪;
//目的数据
公共字符串patient_id_from_intent、patient_name_from_intent、patient_dob_from_intent、patient_mobile_from_intent、patient_email_from_intent;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState)
{
//为该碎片膨胀布局
视图=充气机。充气(R.layout.fragment\u sugar\u level\u报告,容器,false);
findViewById(视图);
返回视图;
}
公共void findViewById(视图)
{
view\u constraintLayout=(constraintLayout)view.findViewById(R.id.view\u constraintLayout);
/*progressBar=(progressBar)view.findViewById(R.id.progressBar)*/
血糖回收视图=(回收视图)视图.findViewById(R.id.blood\u sugar\u recyclerView);
血糖回收视图。setHasFixedSize(真);
血糖回收视图.setLayoutManager(新的LinearLayoutManager(getActivity());
bloodSugaradapter=新的SugarLevelReportAdapter(getActivity(),patientRecordModelArrayList);
血糖回收观察仪(血糖仪);
loadData();
}
公共void loadData()
{
if(GlobalMethods.isNetworkConnected(getActivity()))
{
字符串[]名称=getResources().getStringArray(R.array.names);
for(int i=0;i
适配器类

public class SugarLevelReportAdapter extends RecyclerView.Adapter<SugarLevelReportAdapter.SugarLevelReportHolder>{
public Context context;
public ArrayList<PatientRecordModel> patientRecordModelArrayList = new ArrayList<PatientRecordModel>();

public SugarLevelReportAdapter(Context context, ArrayList<PatientRecordModel> patientRecordModelArrayList)
{
    this.context = context;
    this.patientRecordModelArrayList = patientRecordModelArrayList;
}
@Override
public SugarLevelReportHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sugar_level_report_cardview, parent, false);
    SugarLevelReportHolder sugarLevelReportHolder = new SugarLevelReportHolder(view);
    return sugarLevelReportHolder;
}
@Override
public void onBindViewHolder(SugarLevelReportHolder holder, int position)
{
    PatientRecordModel patientRecordModel = patientRecordModelArrayList.get(position);

    Log.e("patient_name",patientRecordModel.getPatient_name());
    holder.patient_name_textView.setText(patientRecordModel.getPatient_name());
}
@Override
public int getItemCount()
{
    return patientRecordModelArrayList.size();
}
public static class SugarLevelReportHolder extends RecyclerView.ViewHolder
{
    public TextView month_textView, date_textView, year_textView;
    public TextView patient_name_textView, patient_count_textView, meal_type_textView;
    public ImageView overflow_menu_imageView;
    public SugarLevelReportHolder(View itemView)
    {
        super(itemView);
        month_textView = (TextView)itemView.findViewById(R.id.month_textView);
        date_textView = (TextView)itemView.findViewById(R.id.date_textView);
        year_textView = (TextView)itemView.findViewById(R.id.year_textView);
        patient_name_textView = (TextView)itemView.findViewById(R.id.patient_name_textView);
        patient_count_textView = (TextView)itemView.findViewById(R.id.patient_count_textView);
        meal_type_textView = (TextView)itemView.findViewById(R.id.meal_type_textView);
        overflow_menu_imageView = (ImageView)itemView.findViewById(R.id.overflow_menu_imageView);
    }
}
公共类SugarLevelReportAdapter扩展了RecyclerView.Adapter{
公共语境;
public ArrayList patientRecordModelArrayList=新建ArrayList();
公共SugarLevelReportAdapter(上下文上下文,ArrayList patientRecordModelArrayList)
{
this.context=上下文;
this.patientRecordModelArrayList=patientRecordModelArrayList;
}
@凌驾
public SugarLevelReportHolder onCreateViewHolder(视图组父级,int-viewType)
{
查看视图=LayoutFlater.from(parent.getContext()).flate(R.layout.sugar\u level\u report\u cardview,parent,false);
SugarLevelReportHolder SugarLevelReportHolder=新的SugarLevelReportHolder(视图);
返回报告持有人;
}
@凌驾
BindViewHolder上的公共无效(SugarLevelReportHolder,内部位置)
{
PatientRecordModel PatientRecordModel=patientRecordModelArrayList.get(位置);
Log.e(“patient_name”,patientRecordModel.getPatient_name());
holder.patient_name_textView.setText(patientRecordModel.getPatient_name());
}
@凌驾
public int getItemCount()
{
返回patientRecordModelArrayList.size();
}
公共静态类SugarLevelReportHolder扩展了RecyclerView.ViewHolder
{
公共文本视图月份文本视图、日期文本视图、年份文本视图;
公共文本视图患者名称文本视图、患者计数文本视图、膳食类型文本视图;
公共图像视图溢出\u菜单\u图像视图;
公共SugarLevelReportHolder(视图项视图)
{
超级(项目视图);
month_textView=(textView)itemView.findViewById(R.id.month_textView);
date_textView=(textView)itemView.findViewById(R.id.date_textView);
year\u textView=(textView)itemView.findViewById(R.id.year\u textView);
患者\姓名\文本视图=(文本视图)项目视图.findViewById(R.id.patient\姓名\文本视图);
patient_count_textView=(textView)itemView.findViewById(R.id.patient_count_textView);
餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型餐型;
溢出\菜单\图像视图=(图像视图)itemView.findViewById(R.id.overflow\菜单\图像视图);
}
}
}

xml代码[fragment\u sugar\u level\u report.xml]

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/blood_sugar_recyclerView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:scrollbars="vertical"
    android:scrollbarSize="1dp"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp">
</android.support.v7.widget.RecyclerView>


因此,我在当前项目中遇到了类似的问题。我无法告诉您为什么会发生这种情况,但要解决它,您需要将用于更新数据的部件移动到适配器。 在适配器上创建以下方法:

public void updateData(List<PatientRecordModel> list){
   patientRecordModelArrayList  = list;
   notifyDatasetChanged();
  } 

同时,建议在ViewCreated而不是onCreateView上加载您的视图。还可以使用数据映射库(如GSON或jackson)来提高数据映射的速度和可靠性。查看比较希望这有助于

您能否提供一些与GSON或jackson相关的示例或链接,以提高数据映射的速度和可靠性这将是一个巨大的支持。谢谢。我在帖子中添加了这个链接。如果这个答案对你有用,你介意把它标记为将来帮助他人的答案吗
bloodSugaradapter.updateData(patientRecordModel);