当我重新打开应用数组列表时,没有从排球中存储值时

时间:2019-05-14 12:11:24

标签: java android

这是主要活动代码。 当我有时打开应用程序时,它显示数据,有时不显示。而且我发现arraylist列表有时会为空。

这是主要活动代码。 当我有时打开应用程序时,它显示数据,有时不显示。我发现arraylist列表有时是空的。  排球

    StringRequest r = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

           Log.d("code",response);


            JSONObject obj = null;
            try {
                obj = new JSONObject(response);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            JSONArray gallary = null;
            try {
                gallary = obj.getJSONArray("arr");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            for (int i = 0; i < gallary.length(); i++) {

                JSONObject a = null;
                try {
                    a = gallary.getJSONObject(i);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String PostId = null;
                try {
                    PostId = a.getString("Name");
                    list.add(new dataclass("mmmmm"));

                } catch (JSONException e) {
                    e.printStackTrace();

                }

                Log.d("List", String.valueOf(list));
                goahed();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(MainActivity.this,"error",Toast.LENGTH_LONG).show();
        }
    });

    RequestQueue queue= Volley.newRequestQueue(this);
    queue.add(r);

    RecyclerView rv=findViewById(R.id.demolist);




    list.add(new dataclass("please"));

    for (Integer j=0;j < list.size();j++){
    }



    // Creating Adapter Object

    listdemoAdpter adapter=new listdemoAdpter(this,list);

    //Set layout Manager

    rv.setLayoutManager(new LinearLayoutManager(this));

    // set Adapter

    rv.setAdapter(adapter);

我将凌空库用于api调用 预先感谢

1 个答案:

答案 0 :(得分:0)

if(list.isempty()){
   RecyclerView rv=findViewById(R.id.demolist);

   list.add(new dataclass("please"));

   for (Integer j=0;j < list.size();j++){
    }

   // Creating Adapter Object

   listdemoAdpter adapter=new listdemoAdpter(this,list);

   //Set layout Manager

   rv.setLayoutManager(new LinearLayoutManager(this));

  // set Adapter

  rv.setAdapter(adapter);


}else{
 //logic when list is empty and request again in volly


}