我无法将对象添加到arrayList

时间:2017-08-12 09:18:28

标签: java android android-fragments arraylist android-asynctask

我正在尝试从服务器下载一个案例列表,然后填充一个arraylist以在我的RecyclerView适配器中使用它,但每次我尝试从asynctask填充数组列表时,我可以从每一步打印数据填充bu不是来自我正在使用的Arraylist(我正在尝试存储在包含我创建的Case类型的对象的arrayList中)

这是带有问题的代码的一部分                           这是我的AsyncTask ...... `

 public class downloadingNewCases extends AsyncTask<Void,Void ,Boolean>{
        private String userid ;
        private String state ;
        public downloadingNewCases(String userid ,String state ){
            this.userid = userid ;
            this.state = state ;
        }

@Override
protected void onPreExecute() {
    super.onPreExecute();
    Log.d(AppController.DEBUG, "download New Cases Started");
}
@Override
protected Boolean doInBackground(Void... params){
    Log.d(AppController.DEBUG , "user id in the get cases link.." + userid);
    String url = AppController.ApiUrl + "GetCases?UserName="+ userid +"&stat="+state;
    Log.d(AppController.DEBUG_LINK,url);
try {
JsonArrayRequest request = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
    @Override
    public void onResponse(JSONArray response) {
        Log.d("TEST", String.valueOf(response.length()));
        for (int i = 0; i < response.length(); i++) {
            try {
                Log.d("TEST","downloading data" );
                JSONObject object = response.getJSONObject(i);
                JSONArray specs = object.getJSONArray("Spe");
                ArrayList<Specialities> spe = new ArrayList<>();
                for (int j = 0; j < specs.length(); j++) {
                    Log.d("TEST",specs.getJSONObject(j).getString("SName") );
                    spe.add(new Specialities(null, specs.getJSONObject(j).getString("SName")));
                }
                Case ca = new Case(object.getString("ID")
                        , object.getString("Serial")
                        , object.getString("Name")
                        , object.getString("gender")
                        , object.getString("Age")
                        , object.getString("NatioanlityID")
                        , object.getString("HealthProblem")
                        , object.getString("CityID")
                        , object.getString("Problem")
                        , object.getString("Date")
                        , object.getString("Status")
                        , spe);

                Log.d("TEST",ca.toString());

                cases.add(ca);
                spe.clear();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError volleyError) {
        Log.d(AppController.DEBUG , volleyError.toString());

    }
});
request.setRetryPolicy(new DefaultRetryPolicy(
        0,
        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
MySingleton.getmInstance(getActivity()).addToRequestQueue(request);

}catch (Exception e){
Log.d(AppController.DEBUG,"error in the asynctask in the  new cases freagment... ");
Log.d(AppController.DEBUG ,e.toString());
}
    Log.d(AppController.DEBUG, "Cases size ::" + cases.size());
    return true;
}
@Override
protected void onPostExecute(Boolean bool) {
    super.onPostExecute(bool);

/*    if(listener != null){
        listener.getCases(cases);
    }*/
    adapter.notifyDataSetChanged();
 //   Log.d(AppController.DEBUG, "new Cases Downloaded...");


}

} ` (我确定适配器和回收站视图代码是正确的,因为我现在可以使用相同的代码,但是当我再次测试它时它不再起作用了,我不记得我是否更改了一些内容 - 我想不是。)

这是我的onViewCreate方法。

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.doctor_new_cases_fragment,container,false);
    Log.d(AppController.DEBUG_SHARED,"userID in the new case activity ..." + userID);
    SharedPreferences prefs = getActivity().getSharedPreferences(AppController.PREFERENCES_NAME , Context.MODE_PRIVATE);
    userID = prefs.getString(DoctorProfileSubmitActivity.Shared_userid,null) ;
    downloadingNewCases tast  = new downloadingNewCases(userID ,"");
    tast.execute();

    recyclerView = (RecyclerView)view.findViewById(R.id.recyclervew_new_cases);
    adapter = new CasesAdapter(view.getContext(), cases);
    recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(adapter);

    ViewGroup parent = (ViewGroup) view.getParent();
    if (parent != null){
        parent.removeView(view);
    }
    return view;
}

最后将Array列表声明为类的实例变量(类扩展Fragment +它是我在视图分页器中使用的片段,作为我的选项卡Activity的选项卡布局。) 这是ArrayList声明

private final ArrayList<Case> cases = new ArrayList<>();

1 个答案:

答案 0 :(得分:0)

由于您说您可以在每一步都打印数据,因此问题可能与您的数据无关。问题可能是你的案例Arraylist和你的CaseAdapter。

让我们尝试一个简单的测试,而不是用case对象填充你的适配器,创建一个新的字符串ArrayList并在你的响应中用object.getString(&#34; name&#34;)填充它。然后更改您的oncreateview以使用普通字符串arrayadapter显示此名称列表。

如果您可以成功显示名称,这意味着您的案例和案例适应器中存在错误,您需要显示更多代码供我们帮助。