gpt4 book ai didi

android - RecyclerView 用 Volley 请求填充它不显示结果

转载 作者:行者123 更新时间:2023-11-29 15:38:13 26 4
gpt4 key购买 nike

我在使用 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);


}

最佳答案

在 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);

关于android - RecyclerView 用 Volley 请求填充它不显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45927557/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com