添加元素后ArrayList为空 [英] ArrayList being empty after adding elements
问题描述
我有这个 android 代码,它从服务器带来一个 JSON 并从那个 JSON 填充一个 ArrayList我检查了 onresponse void 中 ArrayListmeals"的大小,它给了我 1 但是当我在 StringRequest 对象之后检查它时,我得到了 0 个项目.餐点在全局范围内定义并在 oncreateview 函数内初始化代码:
I have this android code which bring a JSON from a server and fill an ArrayList from that JSON I checked the size of the ArrayList "meals" inside the onresponse void it gives me 1 but when i check it after the StringRequest object i get 0 items . meals is defined in global scope and initialized inside the oncreateview function The Code:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
Log.i("debug","on create view");
View view =inflater.inflate(R.layout.fragment_meal_list,container,false);
ListView List ;
meals=new ArrayList<meal>();
String url="http://syriankitchen.tk/get_recent.php";
StringRequest mealsrequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject object= new JSONObject(response);
JSONArray mealsArray = object.getJSONArray("result");
for(int i=0;i<mealsArray.length();i++){
JSONObject cur = mealsArray.getJSONObject(i);
int id= cur.getInt("id");
String name= cur.getString("name");
String description = cur.getString("description");
int price = cur.getInt("price");
meals.add(new meal(id,name,price,description));
}
Log.i("debug","meals size = "+meals.size());
}
catch(JSONException e){
e.printStackTrace();
}
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(),error.getMessage(),Toast.LENGTH_SHORT).show();
}
});
Volley.newRequestQueue(getActivity()).add(mealsrequest);
ArrayList<String> strs=new ArrayList<String>();
String mealsnames[]=new String[meals.size()];
for(int i=0;i<meals.size();i++)strs.add(meals.get(i).getName());
strs.toArray(mealsnames);
Log.i("debug","meals ou size "+meals.size());
CustomList adapter = new CustomList(getActivity(),mealsnames,meals);
List = (ListView)view.findViewById(R.id.list);
List.setAdapter(adapter);
推荐答案
这里的问题是关于理解异步
任务是如何工作的.当您向队列添加 volley
请求时,它将在后台线程(主线程之外)中运行,并且控制权将传递到下一行.
The problem here is about understanding how asynchronous
tasks work. When you are adding a volley
request to the queque, it will run in background thread (off the main thread) and control will pass to the next line.
所以,在此之后:
Volley.newRequestQueue(getActivity()).add(mealsrequest);
控制权传递给这个:
ArrayList<String> strs=new ArrayList<String>();
String mealsnames[]=new String[meals.size()];
现在由于 meals
在后台线程上更新,您无法在控制到达 String Mealsnames[]=new String[meals.size()] 时获取数据;
Now since meals
is updated on the background thread, you are not able to get the data by the time control reaches String mealsnames[]=new String[meals.size()];
所以你会在这里得到零尺寸(meals.size()
).
So you will get zero size (meals.size()
) here.
尝试将这部分代码移动到 onResponse
中.
Try to move this portion of the code into onResponse
.
试试这个:
public void updateData(){
ArrayList<String> strs=new ArrayList<String>();
String mealsnames[]=new String[meals.size()];
for(int i=0;i<meals.size();i++)strs.add(meals.get(i).getName());
strs.toArray(mealsnames);
Log.i("debug","meals ou size "+meals.size());
CustomList adapter = new CustomList(getActivity(),mealsnames,meals);
List = (ListView)view.findViewById(R.id.list);
List.setAdapter(adapter);
}
并从 onResponse
调用此方法:
@Override
public void onResponse(String response) {
try{
JSONObject object= new JSONObject(response);
JSONArray mealsArray = object.getJSONArray("result");
for(int i=0;i<mealsArray.length();i++){
JSONObject cur = mealsArray.getJSONObject(i);
int id= cur.getInt("id");
String name= cur.getString("name");
String description = cur.getString("description");
int price = cur.getInt("price");
meals.add(new meal(id,name,price,description));
}
Log.i("debug","meals size = "+meals.size());
updateData();
}
catch(JSONException e){
e.printStackTrace();
}
}
这篇关于添加元素后ArrayList为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!