2017-04-11 75 views
-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> 

的截图是这样的:

enter image description here

名单结果是低于这个曲线图:图像,记录/完成/待定任务,但我没有收到。

可以做些什么来解决这个问题?

+0

发布列表项xml并在onBindViewHolder中放置一个断点并查看它的调用与否? – Raghavendra

+0

@ user12345我已经添加了该信息。 –

+0

@SatanPandeya这是一个打字错误看起来像你有一个额外的在fragment_profile_item.xml? – Raghavendra

回答

1

在setAdapter之前设置LayoutManager。

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

它是完全可读的。我错过了这部分。 –