我有这个Android代码,它从服务器带来一个JSON并从该JSON填充一个ArrayList 我检查了ArrayList的大小"饭菜"在onresponse void里面它给了我1但是当我在StringRequest对象之后检查它时我得到0项。 膳食在全球范围内定义并在oncreateview函数内初始化 守则:
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);
答案 0 :(得分:4)
这里的问题是了解asynchronous
任务的工作原理。当你向queque添加volley
请求时,它将在后台线程(在主线程之外)运行,控件将传递到下一行。
所以,在此之后:
Volley.newRequestQueue(getActivity()).add(mealsrequest);
控制传递到:
ArrayList<String> strs=new ArrayList<String>();
String mealsnames[]=new String[meals.size()];
现在,由于meals
在后台线程上更新,因此您无法在时间控件到达String mealsnames[]=new String[meals.size()];
所以这里你会得到零尺寸(meals.size()
)。
尝试将此部分代码移至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();
}
}
答案 1 :(得分:2)
当你写这个 -
Volley.newRequestQueue(getActivity()).add(mealsrequest);
这意味着您正在进行异步调用,mealsrequest
将在另一个线程上运行。
你正在打印 -
Log.i("debug","meals ou size "+meals.size());
在你制作mealsrequest
之后。当控制到达此语句时,您的网络请求尚未完成。显然,你的清单中没有任何东西。您的列表将仅填入onResponse()
,因为该方法在网络请求完成后执行。