【发布时间】:2016-05-11 11:48:30
【问题描述】:
正如标题所说,我的变量在AsyncTask onPostExecute() 上没有更新
这里是代码
public class Search extends AppCompatActivity {
ArrayList<paire> Sectors = new ArrayList<paire>();//paire is my proper class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Rechercher rech = new Rechercher();
rech.execute();
//PRINTING SIZE OF Sectors HERE TELLS ME EXACTLY THE SIZE OF 0
/*
*
*
BLA BLA BLA BLA
*
*
*/
}
public class Rechercher extends AsyncTask<String, Void, ArrayList<paire>>{
@Override
protected ArrayList<paire>doInBackground(String... strings) {
/*
*
Process of creating the arrayList myArray
*
*/
onPostExecute(myArray);//I put this desesperatly xD it doesn't change anything
return myArray;
}
protected void onPostExecute(ArrayList<paire>myArray) {
for (int u = 0; u < myArray.size(); u ++){
paire r = myArray.get(u);
Sectors.add(r);
}
//PRINTING SIZE OF Sectors HERE TELLS ME EXACTLY THE SIZE OF myArray
}
}
}
myArray 是根据从数据库获取的数据(HTTP 连接、JSON 结果...)创建的,结果是非常可接受的 JSON 输出;这里没有问题,只是如果我尝试在 Main 上使用 Sectors 并没有更新。
我不知道我是否真的了解 onPostExecute;或者有问题!
谢谢
【问题讨论】:
-
我认为您对异步执行 asynctask 的概念感到困惑。如果您在调试模式下运行代码并在各种方法中放置断点,您会看到它是如何安排的。
标签: android arrays json android-asynctask