关于Android java编程,一个全局变量 list 在内部类中被修改,但是最后list中添加的内容出了内部类没有了
网络请求的数据能用GSON解析出来,但是不能被赋值到listpublicArrayList<CityEntity>list=newArrayList<CityEntity...
网络请求的数据能用GSON解析出来,但是不能被赋值到list
public ArrayList<CityEntity> list=new ArrayList<CityEntity>()
public class AsyncHttpCity {
public static ArrayList<CityEntity> list=new ArrayList<CityEntity>();
/**
* 1.应该不用额外写一个登录的方法,因为本界面是通过主界面跳转过来的,而主界面已经通过了登录
* 2.但是当登录过期的时候可能需要从新认证登录···不知道是否要重新写
*/
public AsyncHttpCity(ArrayList<CityEntity> list){
super();
if(list!=null){
this.list=list;
}
else {
this.list=new ArrayList<CityEntity>();
}
}
public ArrayList<CityEntity> asynchttpcityPost(String city) {
AsyncHttpClient client = new AsyncHttpClient();
// url="";
String url = "";
RequestParams params = new RequestParams();
params.put("user_id", "1");
params.put("md5_code", "facdd148a7d4c75ebdcb60b0716bc590");
params.put("city", city);
client.post(url, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String arg0) {
System.out.println("===AsyncHttpCity=====里面里面里面");
super.onSuccess(arg0);
Gson gson = new Gson();
CityList citylist = gson.fromJson(arg0, CityList.class);
for (int i = 0; i < citylist.content.size(); i++) {
String name=citylist.getContent().get(i).getCityName();
CityEntity cityEntity=new CityEntity();
cityEntity.setName(name);
list.add(0,cityEntity);
Log.i("城市里面的列表", "ist.size()=="+list.size());
}
}
@Override
public void onFailure(Throwable arg0) {
Log.i("错误信息", "网络请求失败:" + arg0);
super.onFailure(arg0);
}
});
Log.i("list.size()", "list.size() of AsyncHttpCity==="+list.size());
return list;
}
} 展开
public ArrayList<CityEntity> list=new ArrayList<CityEntity>()
public class AsyncHttpCity {
public static ArrayList<CityEntity> list=new ArrayList<CityEntity>();
/**
* 1.应该不用额外写一个登录的方法,因为本界面是通过主界面跳转过来的,而主界面已经通过了登录
* 2.但是当登录过期的时候可能需要从新认证登录···不知道是否要重新写
*/
public AsyncHttpCity(ArrayList<CityEntity> list){
super();
if(list!=null){
this.list=list;
}
else {
this.list=new ArrayList<CityEntity>();
}
}
public ArrayList<CityEntity> asynchttpcityPost(String city) {
AsyncHttpClient client = new AsyncHttpClient();
// url="";
String url = "";
RequestParams params = new RequestParams();
params.put("user_id", "1");
params.put("md5_code", "facdd148a7d4c75ebdcb60b0716bc590");
params.put("city", city);
client.post(url, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String arg0) {
System.out.println("===AsyncHttpCity=====里面里面里面");
super.onSuccess(arg0);
Gson gson = new Gson();
CityList citylist = gson.fromJson(arg0, CityList.class);
for (int i = 0; i < citylist.content.size(); i++) {
String name=citylist.getContent().get(i).getCityName();
CityEntity cityEntity=new CityEntity();
cityEntity.setName(name);
list.add(0,cityEntity);
Log.i("城市里面的列表", "ist.size()=="+list.size());
}
}
@Override
public void onFailure(Throwable arg0) {
Log.i("错误信息", "网络请求失败:" + arg0);
super.onFailure(arg0);
}
});
Log.i("list.size()", "list.size() of AsyncHttpCity==="+list.size());
return list;
}
} 展开
1个回答
展开全部
list.add(0,cityEntity);这个错了吧?
ArrayList的add(int index,String element)
参数:
index :index at which the specified element is to be inserted
element: element to be inserted
是不是所有数据都插入到第1个了
ArrayList的add(int index,String element)
参数:
index :index at which the specified element is to be inserted
element: element to be inserted
是不是所有数据都插入到第1个了
更多追问追答
追问
是啊,但是插入之后,原本的内容不是被移到后面去吗,而且即使被覆盖list也不会为空啊,现在出了内部类之后,list就变空了。
追答
是不是获取数据的代码在onsuccess方法之前执行了呢?
猜的。我没弄过安卓不清楚这个框架,不过异步的话可能是这个原因。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询