使用改造2 android解析嵌套的json

时间:2017-04-11 10:56:37

标签: android json retrofit2

这是我的json:

{
  "status": "success",
  "data": {
    "totalEstimation": 15,
    "completed": 0,
    "pending": 15,
    "daily_effort": [
      {
        "taskName": "in progress",
        "projectName": "Test Project",
        "estimatedTime": "30",
        "timeLog": "15",
        "estimationVariation": 50
      },
      {
        "taskName": "po review",
        "projectName": "Test Project",
        "estimatedTime": "30",
        "timeLog": "45",
        "estimationVariation": 150
      }
    ]
  }
}

我的模特课是:

public class ProfileResponse {
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("data")
    @Expose
    private Data data;
    // getter and setter
}

public class Data {
    @SerializedName("totalEstimation")
    @Expose
    private Integer totalEstimation;
    @SerializedName("completed")
    @Expose
    private Integer completed;
    @SerializedName("pending")
    @Expose
    private Integer pending;
    @SerializedName("daily_effort")
    @Expose
    private List<DailyEffort> dailyEffort = null;
    //getter and setter
}

public class DailyEffort {
    @SerializedName("taskName")
    @Expose
    private String taskName;
    @SerializedName("projectName")
    @Expose
    private String projectName;
    @SerializedName("estimatedTime")
    @Expose
    private String estimatedTime;
    @SerializedName("timeLog")
    @Expose
    private String timeLog;
    @SerializedName("estimationVariation")
    @Expose
    private Integer estimationVariation;
    // getter and setter
}

我的适配器类:

public class ProfileAdapter extends RecyclerView.Adapter<ProfileAdapter.MyViewHolderClass> {
private Context context;
private List<DailyEffort> dailyEffortList = new ArrayList<>();

public ProfileAdapter(Context context, List<DailyEffort> dailyEffortList) {
    this.context = context;
    this.dailyEffortList = dailyEffortList;
}

@Override
public MyViewHolderClass onCreateViewHolder(ViewGroup parent, int viewType) {
    View profileContainer = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_profile_item, parent, false);
    return new MyViewHolderClass(profileContainer);
}

@Override
public void onBindViewHolder(MyViewHolderClass holder, int position) {
    DailyEffort dailyEffort = dailyEffortList.get(position);
    holder.taskName.setText(dailyEffort.getTaskName());
    holder.projectName.setText(dailyEffort.getProjectName());
    holder.timeLog.setText(dailyEffort.getTimeLog());
    boolean estimationVariation = dailyEffort.getEstimationVariation() >= 0;
    if (estimationVariation) {
        holder.estimationUp.setVisibility(View.VISIBLE);
        holder.estimationUp.setText("+" + dailyEffort.getEstimationVariation() + "");
    } else {
        holder.estimationDown.setVisibility(View.VISIBLE);
        holder.estimationDown.setText("-" + dailyEffort.getEstimationVariation() + "");
    }

}

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


public class MyViewHolderClass extends RecyclerView.ViewHolder {
    @BindView(R.id.proTaskNameId)
    TextView taskName;
    @BindView(R.id.proProjectNameId)
    Button projectName;
    @BindView(R.id.proLogTimeId)
    Button timeLog;
    @BindView(R.id.proEstimationVariationUpId)
    Button estimationUp;
    @BindView(R.id.proEstimationVariationDownId)
    Button estimationDown;
    @BindView(R.id.proDividerLineId)
    View dividerLine;

    public MyViewHolderClass(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }
}
}

在我的片段类中:

Call<ProfileResponse> profileResponseCall = profileService.profileContainer(preferenceClass.getLoginToken());
    profileResponseCall.enqueue(new Callback<ProfileResponse>() {
        @Override
        public void onResponse(Call<ProfileResponse> call, Response<ProfileResponse> response) {
            if (response.isSuccessful()){
                ProfileResponse profileResponse = response.body();
                if (profileResponse.getData() != null && profileResponse.getData().getDailyEffort() != null) {
                    profilePresenter.setDismiss();
                    logTime.setText("" + String.valueOf(profileResponse.getData().getTotalEstimation()) + " hrs");
                    completedTime.setText("" + String.valueOf(profileResponse.getData().getCompleted()) + " hrs");
                    pendingTime.setText("" + String.valueOf(profileResponse.getData().getPending()) + " hrs");

                    dailyEffortList = profileResponse.getData().getDailyEffort();


                    profileAdapter = new ProfileAdapter(getContext(), dailyEffortList);
                    recyclerViewProfile.setAdapter(profileAdapter);

                } 

            } 

        }

        @Override
        public void onFailure(Call<ProfileResponse> call, Throwable t) {

        }
    });

布局文件:

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/profileRl"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:background="@color/colorProfileBackgroundColor">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/developerIconId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
          />

        <TextView
            android:id="@+id/developerNameId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/developerTagId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

        <TextView
            android:id="@+id/loggedTimeID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
          />

        <TextView
            android:id="@+id/loggedTextId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           />

        <TextView
            android:id="@+id/completedTimeId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

        <TextView
            android:id="@+id/completedTextId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
          />

        <TextView
            android:id="@+id/pendingTimeId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

        <TextView
            android:id="@+id/pendingTextId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
          />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/profileRl">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerViewProfileId"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>


</RelativeLayout>

我正在从服务器获取记录时间,完成时间,待定时间。但是,RecyclerView没有显示。

当我使用log在控制台中查看结果时,它会显示数据。

Log.e(TAG, "onResponse: "+profileResponse.getData().getDailyEffort().get(0).getTaskName());
Log.e(TAG, "onResponse: "+profileResponse.getData().getDailyEffort().get(1).getTaskName());

这是适配器类的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp">

<RelativeLayout
    android:id="@+id/rlOne"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">

    <TextView
        android:id="@+id/proTaskNameId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="66dp"
        android:layout_marginStart="10dp"
        android:textColor="@color/colorHomeToolBarAndTab"
        android:textSize="16sp" />

</RelativeLayout>

<RelativeLayout
    android:id="@+id/rlTwo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rlOne">

    <Button
        android:id="@+id/proProjectNameId"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:textColor="@color/colorButtonTextColor" />

    <Button
        android:id="@+id/proLogTimeId"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_toEndOf="@+id/proProjectNameId"
        android:drawablePadding="3dp"
        android:drawableStart="@drawable/ic_watch"
        android:textColor="@color/colorButtonTextColor"

        />

    <Button
        android:id="@+id/proEstimationVariationUpId"
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:layout_toEndOf="@+id/proLogTimeId"
        android:textColor="@color/colorButtonTextColor"
        android:drawableStart="@drawable/ic_drawable_triangle_up"
        android:visibility="gone"
        />
    <Button
        android:id="@+id/proEstimationVariationDownId"
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:layout_toEndOf="@+id/proLogTimeId"
        android:textColor="@color/colorButtonTextColor"
        android:drawableStart="@drawable/ic_drawable_triangle_up"
        android:visibility="gone"
        />

</RelativeLayout>

<View
    android:id="@+id/proDividerLineId"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_below="@+id/rlTwo"
    android:layout_marginEnd="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="10dp"
    android:background="@color/colorRecyclerViewDividerColor" />

</RelativeLayout>

屏幕截图如下:

enter image description here

列表结果位于此配置文件视图下方:图像,已记录/已完成/待处理任务,但我没有收到。

可以采取哪些措施来解决这个问题?

1 个答案:

答案 0 :(得分:1)

在setAdapter之前设置LayoutManager。

recyclerViewProfile.setLayoutManager(new LinearLayoutManager(getContext()));