模型类中的数组列表,但清除了数组列表

时间:2018-08-13 11:56:22

标签: android multidimensional-array

我只是用model class创建了arraylist。但是,当数据设置为模型时,最后的索引值数据将存储在模型中。 对所有列表对象重复设置的最后一个响应索引值 主模型列表清晰,但数组列表不清楚。 抱歉语法错误,这是我在堆栈上的第一个问题。

这是我的活动代码

List<Search> search;
List<SegmentData> segmentData;


//on create

private void search()
{
    //webservice
    postJsonObject,
        new Response.Listener<JSONObject>()
        {
            @Override
            public void onResponse(JSONObject jsonObject)
            {
                loading.dismiss();
                if(search==null)
                {
                    search=new ArrayList<>();
                }
                else
                {
                    search.clear();
                }


                try
                {
                    String err_msg=jsonObject.getJSONObject("Response").getJSONObject("Error").getString("ErrorCode");
                    String message=jsonObject.getJSONObject("Response").getJSONObject("Error").getString("ErrorMessage");


                    if (err_msg.equals("0"))
                    {
                        JSONObject main_res=jsonObject.getJSONObject("Response");
                        JSONArray result_list=main_res.getJSONArray("Results").getJSONArray(0);
                        if (result_list!=null)
                        {
                            for (int i = 0; i < result_list.length();i++)
                            {
                                if (i==3)
                                {
                                    break;
                                }
                                //search model having SegmentData arraylist
                                Search s_search=new Search();
                                s_search.setTraceId(jsonObject.getJSONObject("Response").getString("TraceId"));
                                //main object
                                JSONObject index=result_list.getJSONObject(i);
                                s_search.setI_ResultIndex(index.getString("ResultIndex"));
                                s_search.setI_IsRefundable(index.getString("IsRefundable"));
                                s_search.setI_AirlineCode(index.getString("AirlineCode"));
                                //Segments


                                JSONArray segArray= index.getJSONArray("Segments");
                                for (int h = 0; h < segArray.length();h++)
                                {

                                    JSONArray one = segArray.getJSONArray(h);

                                    if (segmentData==null)
                                    {
                                        segmentData=new ArrayList<>();
                                    }
                                    else 
                                    {
                                        segmentData.clear();
                                    }

                                    for (int k = 0; k < one.length(); k++)
                                    {
                                        JSONObject ram = one.getJSONObject(k);
                                        SegmentData SD = new SegmentData();
                                        SD.setBaggage(ram.getString("Baggage"));                                           
                                        SD.setStopPointArrivalTime(ram.getString("StopPointArrivalTime"));
                                        SD.setStopPointDepartureTime(ram.getString("StopPointDepartureTime"));
                                        SD.setCraft(ram.getString("Craft"));

                                        segmentData.add(SD);
                                        Log.e("parag"," segmentData size : "+segmentData.size()+"");
                                    }
                                    s_search.setSegmentData(segmentData);

                                }

                                search.add(s_search);


                            }
                            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(SeachList.this);
                            searchListAdapter = new SearchListAdapter(SeachList.this, search);
                            recycler_view.setAdapter(searchListAdapter);
                            recycler_view.setLayoutManager(mLayoutManager);



                        }
                        else
                        {
                            Toast.makeText(SeachList.this, message, Toast.LENGTH_SHORT).show();
                            loading.dismiss();
                        }

                    }
                    else
                    {
                        Toast.makeText(SeachList.this, message, Toast.LENGTH_SHORT).show();
                        loading.dismiss();

                    }

                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    Log.e("Exception",e.toString());
                    loading.dismiss();

                }
            }
        }, new Response.ErrorListener()
    {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Log.e("Message from server", volleyError.toString());
            Toast.makeText(SeachList.this, "Error Occurred", Toast.LENGTH_SHORT).show();

            Log.e("parag ","rsponse  error : "+volleyError.toString());

            loading.dismiss();

        }
    })
            ;
    jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy( 200*30000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    Volley.newRequestQueue(SeachList.this).add(jsonObjectRequest);
  }
}

0 个答案:

没有答案