如何使用给定的json使用改进2解析recyclelerview上的数据

时间:2017-10-31 12:17:19

标签: android json retrofit

Hello im new with retrofit和json。我有一个给定的json,并希望在recyclerview中显示数据。我试图在getWorkQueue()中解析数据,但出了点问题。

myJson

[
    {
        ""REQUEST_NO"": ""CR-201718-6"",
        ""PROCESS_NAME"": ""CONVEYANCE REIMBURSEMENT"",
        ""STEPNAME"": null,
        ""PROCESSID"": 1,
        ""INSTANCEID"": 6,
        ""AMOUNT"": 250,
        ""RAISED_BY"": ""flologic1"",
        ""RAISED_DATE"": ""12-Sep-2017"",
        ""APPROVED_BY"": ""Flologic2"",
        ""APPROVED_DATE"": null,
        ""BRANCH"": ""101""
    }
]

WorkQueueAdapter.java

公共类WorkQueueAdapter扩展了RecyclerView.Adapter {

private List<TravelWorkQueue> mDataset;
private Context context;

public WorkQueueAdapter(List<TravelWorkQueue> myDataset, Context mContext) {
    this.mDataset = myDataset;
    this.context = mContext;
}

@Override
public void onBindViewHolder(DataObjectHolder viewHolder, final int position) {

    TravelWorkQueue queue = mDataset.get(position);
    viewHolder.raisedBy.setText(queue.getRaisedBy());
    viewHolder.requestNo.setText(queue.getRequestNo());
    viewHolder.trdate.setText(queue.getRaisedDate());
    viewHolder.processName.setText(queue.getProcessName());

}

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

public static class DataObjectHolder extends RecyclerView.ViewHolder {

    TextView raisedBy, requestNo, trdate, processName;
    LinearLayout linearLayout;

    public DataObjectHolder(View itemView) {
        super(itemView);
        raisedBy = (TextView) itemView.findViewById(R.id.raisedBy);
        requestNo = (TextView) itemView.findViewById(R.id.requestNo);
        trdate = (TextView) itemView.findViewById(R.id.trDate);
        processName = (TextView) itemView.findViewById(R.id.processName);

        linearLayout = (LinearLayout) itemView.findViewById(R.id.linearIteam);
    }

}

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

}

片段类

公共类WorkQueue扩展了Fragment {

public static final String TAG = WorkQueue.class.getSimpleName();
private View rootView;
private ListView myRequestList;
private RecyclerView.LayoutManager mLayoutManager;
private WorkQueueAdapter mAdapter;

private RecyclerView mRecyclerView;
String userName = "flologic2";

private TravelWorkQueue travelWorkQueue;
List<TravelWorkQueue> travelWorkQueues;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.work_queue, container, false);

    //  userName = getParent().getUserName();
    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);

    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);
    travelWorkQueues = new ArrayList<>();
    // TravelRequestService  travelRequestService = HDFCService.getTravelRequestService();

    getWorkQueue();

    return rootView;
}

private HomeActivity getParent() {
    return (HomeActivity) getActivity();
}

private void getWorkQueue() {

    Call<List<TravelWorkQueue>> call = HDFCService.getTravelRequestService().getRequestQueqeDetails(userName);
    call.enqueue(new Callback<List<TravelWorkQueue>>() {
        @Override
        public void onResponse(Call<List<TravelWorkQueue>> call, Response<List<TravelWorkQueue>> response) {

            List<TravelWorkQueue> list = response.body();
            TravelWorkQueue workQueue = null;

            //int i=0;
           for (int i = 0; i < list.size(); i++) {
                workQueue = new TravelWorkQueue();
                String requestNo = list.get(i).getRequestNo();
                String raisedBy = list.get(i).getApproverBy();
                String raisedDate = list.get(i).getRaisedDate();
                String processName = list.get(i).getProcessName();

                workQueue.setRequestNo(requestNo);
                workQueue.setRaisedBy(raisedBy);
                workQueue.setRaisedDate(raisedDate);
                workQueue.setProcessName(processName);

                list.add(workQueue);
            i++;


                mAdapter = new WorkQueueAdapter(travelWorkQueues, getActivity());
                mRecyclerView.setAdapter(mAdapter);
            }
        }

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

            Toast.makeText(getActivity(), "Failed", Toast.LENGTH_LONG).show();
        }
    });}}

3 个答案:

答案 0 :(得分:4)

问题:

  • 内部循环不需要i++
  • 创建并设置适配器外部循环
  • travelWorkQueues没有数据,您要将数据添加到list,而是将数据添加到travelWorkQueues 列表,或者在创建适配器时使用list引用

for (int i = 0; i < list.size(); i++) {
    // code
    //i++;

//    mAdapter = new WorkQueueAdapter(travelWorkQueues, getActivity());
//    mRecyclerView.setAdapter(mAdapter);
      travelWorkQueues.add(workQueue);
}
    mAdapter = new WorkQueueAdapter(travelWorkQueues, getActivity());
    mRecyclerView.setAdapter(mAdapter);

答案 1 :(得分:1)

1.从for循环中删除i ++,因为它已在for循环语句中递增

2.将数据添加到&#34; travelWorkQueues&#34;并删除本地列表&#34; list&#34;因为它是多余的或删除列表&#34; travelWorkQueues&#34;并添加pass&#34; list&#34;如 @Pavneet_Singh提到。

3.将mAdapter创建移到for循环外面,因为现在每次都在创建适配器的新实例。

       for (int i = 0; i < list.size(); i++) {
            workQueue = new TravelWorkQueue();
            String requestNo = list.get(i).getRequestNo();
            String raisedBy = list.get(i).getApproverBy();
            String raisedDate = list.get(i).getRaisedDate();
            String processName = list.get(i).getProcessName();

            workQueue.setRequestNo(requestNo);
            workQueue.setRaisedBy(raisedBy);
            workQueue.setRaisedDate(raisedDate);
            workQueue.setProcessName(processName);

            travelWorkQueues.add(workQueue);

               //Remove this  i++
           }
          //Call this after the for loop
          mAdapter = new WorkQueueAdapter(travelWorkQueues, getActivity()); 
          mRecyclerView.setAdapter(mAdapter);

答案 2 :(得分:0)

替换此方法。然后拨打mAdapter.notifydatasetchanged()

@Override
public void onResponse(Call<List<TravelWorkQueue>> call, Response<List<TravelWorkQueue>> response) {
    List<TravelWorkQueue> list = response.body();
    TravelWorkQueue workQueue = null;
    //int i=0;
    for (int i = 0; i < list.size(); i++) {
        workQueue = new TravelWorkQueue();
        String requestNo = list.get(i).getRequestNo();
        String raisedBy = list.get(i).getApproverBy();
        String raisedDate = list.get(i).getRaisedDate();
        String processName = list.get(i).getProcessName();

        workQueue.setRequestNo(requestNo);
        workQueue.setRaisedBy(raisedBy);
        workQueue.setRaisedDate(raisedDate);
        workQueue.setProcessName(processName);
        list.add(workQueue);
    } mAdapter = new WorkQueueAdapter(travelWorkQueues, getActivity());
    mRecyclerView.setAdapter(mAdapter);
}