-2
解析嵌套的JSON這是我的JSON:使用改裝2安卓
{
"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>
的截圖是這樣的:
名單結果是低於這個曲線圖:圖像,記錄/完成/待定任務,但我沒有收到。
可以做些什麼來解決這個問題?
發佈列表項xml並在onBindViewHolder中放置一個斷點並查看它的調用與否? – Raghavendra
@ user12345我已經添加了該信息。 –
@SatanPandeya這是一個打字錯誤看起來像你有一個額外的 RelativeLayout>在fragment_profile_item.xml? – Raghavendra