有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java当我重新打开应用程序arraylist时没有存储来自volley的值

这是主要的活动代码。 当我打开应用程序时,有时会显示数据,有时不会。我发现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);

我使用volley库进行api调用 提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    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
    
    
    }