gpt4 book ai didi

android - 与 Firestore 异步

转载 作者:搜寻专家 更新时间:2023-11-01 09:25:51 24 4
gpt4 key购买 nike

我正在尝试使 ProgressBar 旋转,而来自 Firestore 的数据正在加载到 ListView 中。然后,加载后 - ProgressBar 应该消失。

这就是我尝试做的方式,但在多线程中我仍然是一个完整的零。

它是内部类,我尝试在其中使用异步:

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;
}
}

和 Activity 布局:

<?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 调用一起使用是多余的。

您可以简单地创建两个方法:

public void showLoading(){
prog.setVisibility(View.VISIBLE);
}

public void hideLoading(){
prog.setVisibility(View.GONE);
}

在调用对 firebase 的异步调用之前调用这些方法。

当您收到结果时调用隐藏方法:

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();
}

关于android - 与 Firestore 异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50759413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com