【问题标题】:RecyclerView fill it with a Volley request doesn't show resultsRecyclerView 用 Volley 请求填充它不显示结果
【发布时间】:2017-08-28 21:21:42
【问题描述】:

VolleyRecyclerView 有问题。

在我的ActivityonCreate 方法中,我调用Volley Request 从我的服务器获取数据。然后我将它作为List 传递给我的适配器。

问题是,当onCreate 方法完成时,一切都发生得如此之快,我的Volley Request 还没有完成,所以屏幕上什么都没有显示,我该如何避免这种情况?

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__services__worker);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("Servicios");
        final List services = new ArrayList();

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    StringRequest stringRequest = new StringRequest(Method.GET, AppConfig.URL_GET_SERVICES_REQUIRED, new Response.Listener<String>(){
        @Override
        public void onResponse(String response){
            try{
                JSONArray jsonArray = new JSONArray(response);
                for(int i=0; i<jsonArray.length(); i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String tag_servicio = jsonObject.getString("contratista");
                    String servicio = jsonObject.getString("descipcion_del_problema");
                    String zona = jsonObject.getString("zona");
                    String money = jsonObject.getString("precio_min");
                    //Toast.makeText(Activity_Services_Worker.this, tag_servicio+servicio+zona+money, Toast.LENGTH_SHORT).show();
                    services.add(new Servicios_worker(tag_servicio, servicio, zona, money));
                }
            }catch (JSONException e){
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener(){
        @Override
        public void onErrorResponse (VolleyError error){
            Log.e(TAG, "Registration Error: " + error.getMessage());
            Toast.makeText(Activity_Services_Worker.this, error.getMessage(), Toast.LENGTH_LONG).show();
        }
    }); 
    requestQueue.add(stringRequest);



    recycler = (RecyclerView) findViewById(R.id.servicesAsked_RecyclerView);
    recycler.setHasFixedSize(true);

    lManager = new LinearLayoutManager(this);
    recycler.setLayoutManager(lManager);

    adapter = new Adapter_services_required(services);
    recycler.setAdapter(adapter);


    }

【问题讨论】:

    标签: android android-recyclerview android-volley


    【解决方案1】:

    在 add 方法中,我认为是在适配器的元素数组中添加一个元素,你应该这样做 notifyDatasetChanged() 或者你可以这样做:

    ...
    final List services = new ArrayList();
    
    ;; CHANGED
    recycler = (RecyclerView) findViewById(R.id.servicesAsked_RecyclerView);
    recycler.setHasFixedSize(true);
    
    lManager = new LinearLayoutManager(this);
    recycler.setLayoutManager(lManager);
    
    adapter = new Adapter_services_required(services);
    recycler.setAdapter(adapter);
    
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    StringRequest stringRequest = new StringRequest(Method.GET, AppConfig.URL_GET_SERVICES_REQUIRED, new Response.Listener<String>(){
        @Override
        public void onResponse(String response){
            try{
                JSONArray jsonArray = new JSONArray(response);
                for(int i=0; i<jsonArray.length(); i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String tag_servicio = jsonObject.getString("contratista");
                    String servicio = jsonObject.getString("descipcion_del_problema");
                    String zona = jsonObject.getString("zona");
                    String money = jsonObject.getString("precio_min");
                    //Toast.makeText(Activity_Services_Worker.this, tag_servicio+servicio+zona+money, Toast.LENGTH_SHORT).show();
                    services.add(new Servicios_worker(tag_servicio, servicio, zona, money));
                    ;; CHANGED
                    adapter.notifyDatasetChanged(); 
                }
            }catch (JSONException e){
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener(){
        @Override
        public void onErrorResponse (VolleyError error){
            Log.e(TAG, "Registration Error: " + error.getMessage());
            Toast.makeText(Activity_Services_Worker.this, error.getMessage(), Toast.LENGTH_LONG).show();
        }
    }); 
    requestQueue.add(stringRequest);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 1970-01-01
      • 2022-01-20
      相关资源
      最近更新 更多