与Firestore异步 [英] Async with Firestore
问题描述
我正在尝试使 ProgressBar
旋转,同时将 Firestore
中的数据加载到 ListView
中.然后,加载后- ProgressBar
应该消失.
I'm trying to make ProgressBar
rotate, while data from Firestore
is loading into the ListView
. And then, after loading - the ProgressBar
should disappear.
这就是我尝试的方法,但是在多线程中,我仍然是一个完整的零.
That's how I tried to do it, but in multithreading I'm still a complete zero.
这是内部类,我尝试在其中使用异步:
It's inner class, where I try to use async:
ProgressBar prog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
prog = findViewById(R.id.progressCircle);
prog.setVisibility(View.INVISIBLE);
class MyProgress extends AsyncTask<Void, Integer, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
prog.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
prog.setVisibility(View.INVISIBLE);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected Void doInBackground(Void... voids) {
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<Places> placesList = new ArrayList<>();
for (DocumentSnapshot document : task.getResult()) {
Places places = document.toObject(Places.class);
System.out.println(places);
placesList.add(places);
}
if (placesList.size() > 0) {
System.out.println(placesList);
int placeCount = placesList.size();
Random randomGenerator = new Random();
ArrayList<Places> randomPlaceList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
int randomIndex = randomGenerator.nextInt(placeCount);;
Places item = placesList.get(randomIndex);
randomPlaceList.add(item);
}
ListView mListView = findViewById(R.id.place_list);
mAdapter = new PlaceAdapter(randomPlaceList, getBaseContext());
mListView.setAdapter(mAdapter);
}
} else {
error.setVisibility(View.VISIBLE);
}
}
});
return null;
}
}
活动布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/card_new_bg">
<ProgressBar
android:id="@+id/progressCircle"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<ListView
android:id="@+id/name_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true" >
</ListView>
</RelativeLayout>
推荐答案
将异步任务与按设计是异步的Firebase调用一起使用是相当多余的.
It's rather redundant to use async task with firebase calls which are async by design.
您可以简单地创建两个方法:
What you can simply do is to create two methods:
public void showLoading(){
prog.setVisibility(View.VISIBLE);
}
public void hideLoading(){
prog.setVisibility(View.GONE);
}
在调用异步调用Firebase之前,请先调用这些方法.
And right before calling your async call to firebase call these methods.
收到结果后,调用隐藏方法:
When you receive the result call the hiding method:
public void queryFirebase() {
showLoading();
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<Places> placesList = new ArrayList<>();
for (DocumentSnapshot document : task.getResult()) {
Places places = document.toObject(Places.class);
System.out.println(places);
placesList.add(places);
}
if (placesList.size() > 0) {
int placeCount = placesList.size();
Random randomGenerator = new Random();
ArrayList<Places> randomPlaceList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
int randomIndex = randomGenerator.nextInt(placeCount);;
Places item = placesList.get(randomIndex);
randomPlaceList.add(item);
}
ListView mListView = findViewById(R.id.place_list);
mAdapter = new PlaceAdapter(randomPlaceList, getBaseContext());
mListView.setAdapter(mAdapter);
}
} else {
error.setVisibility(View.VISIBLE);
}
hideLoading();
}
});
}
那你就做:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
prog = findViewById(R.id.progressCircle);
prog.setVisibility(View.INVISIBLE);
queryFirebase();
}
这篇关于与Firestore异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!