我对AsyncTask
有问题,onPostExecute
方法在doInBackground
方法完成工作之前运行。
我正在尝试从Firebase数据库中获取一些数据,将其保存到“ Didattica”对象的数组列表中,以传递给可扩展列表视图,我在数据库上有4个对象。
问题是onPostExecute
在doInBackground
之后不久运行,然后doInBackground
完成了它的工作,填充了arrayList。
public class DidatticaActivity extends AppCompatActivity {
ArrayList<Didattica> reishiki1 = null;
private ExpandableListView listView1;
private ExpandableListAdapter listAdapter1;
private List<String> listDataHeader1;
private HashMap<String, List<String>> listHash1;
AsyncTaskDidattica atDidattica;
final FirebaseDatabase database = FirebaseDatabase.getInstance();
private DatabaseReference refDidattica1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_didattica);
listHash1 = new HashMap<>();
listDataHeader1 = new ArrayList<>();
listView1 = findViewById(R.id.lvReishiki);
atDidattica = new AsyncTaskDidattica();
atDidattica.execute();
listAdapter1 = new ExpandableListAdapter(this, listDataHeader1,listHash1);
listView1.setAdapter(listAdapter1);
}
private class AsyncTaskDidattica extends AsyncTask<String, String, HashMap<String, ArrayList<Didattica>>>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected HashMap<String, ArrayList<Didattica>> doInBackground(String... strings) {
HashMap result = new HashMap<>();
reishiki1 = new ArrayList<>();
try {
refDidattica1 = database.getReference("didattica/1-reishiki");
refDidattica1.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot1) {
Log.i("shinseiLog", "onDataChange" );
for (DataSnapshot child1 : dataSnapshot1.getChildren()) {
Didattica d1 = child1.getValue(Didattica.class);
reishiki1.add(d1);
}
Log.i("shinseiLog", "rei size1: " + reishiki1.size());
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
result.put("reishiki", reishiki1);
Log.i("shinseiLog", "map size 1: " + result.size());
return result;
}catch(Exception e){
Log.i("shinseiLog", "eccezione in doInBackground");
}
return null;
}
@Override
protected void onPostExecute(HashMap<String, ArrayList<Didattica>> result) {
super.onPostExecute(result);
ArrayList<Didattica> rei = result.get("reishiki");
Log.i("shinseiLog", "rei size2: " + rei.size());
for(Didattica r: rei){
listDataHeader1.add(r.getTitolo());
listHash1.put(r.getTitolo(),r.getList());
}
Log.i("shinseiLog", "header: " + listDataHeader1.size());
Log.i("shinseiLog", "hash: " + listHash1.size());
listAdapter1.notifyDataSetChanged();
}
}
}
我添加了一些日志以帮助理解顺序:
I/shinseiLog: map size 1: 1
I/shinseiLog: rei size2: 0
I/shinseiLog: header: 0
I/shinseiLog: hash: 0
I/shinseiLog: onDataChange
I/shinseiLog: rei size1: 4
最后一行正确显示arrayList包含我的所有数据,但是为时已晚。 为什么以这种方式执行?我在哪里错了?
谢谢
答案 0 :(得分:0)
Firebase已经是异步的,您无需使用AsyncTask即可获取或上传数据。
答案 1 :(得分:0)
请记住,firebase是异步的。 所以你的方法...
public void onDataChange(DataSnapshot dataSnapshot1) {
}
...正在另一个线程中执行。这就是onPostExecute
首先执行的原因。
尝试此操作,将onPostexecute
代码中的代码放入方法onDataChange
,see this picture中