gpt4 book ai didi

java - 为什么当 firebase 实时数据库中不存在数据时,progressDialog 不会被关闭?

转载 作者:行者123 更新时间:2023-12-01 18:09:57 24 4
gpt4 key购买 nike

这是我的代码,当 Firebase 上存在数据并提取到 UI 中时,我的 ProgressDialog 会关闭,但是当 Firebase 上没有数据时,它仍在处理,并且当我们点击屏幕时,它不会关闭,所以我怎样才能忽略它?

public void ShowData()//String data)
{
final ProgressDialog progressDialog =new ProgressDialog ( this );
progressDialog.setMessage ( "Loading.." );
progressDialog.show ();

query1 = myRef.orderByChild ( "dateTime" ); // this is for Filter the data (The recently data you add is show on top of application)
// options=new FirebaseRecyclerOptions.Builder<modelClass> ().setQuery ( myRef,modelClass.class ).build ();//for retrive
options = new FirebaseRecyclerOptions.Builder < modelClass > ().setQuery ( query1 , modelClass.class ).build ();//for search and retrive
adapter = new FirebaseRecyclerAdapter < modelClass, MyVIewHolder > ( options ) {

@Override
protected void onBindViewHolder(@NonNull final MyVIewHolder holder , final int position , @NonNull modelClass model) //for binding the View of recycler_view_item_layout
{

/* holder.college.setText ( model.getCollege () );
holder.contact.setText ( model.getContact () );
holder.time.setText ( model.getDateTime ());
holder.food.setText ( model.getFood () );
holder.name.setText ( model.getName () );*/



progressDialog.dismiss ();
holder.setFoodPost ( model ); //for set all data to view



//for set intent on Contact num View
holder.contact.setOnClickListener ( new View.OnClickListener () {
@Override
public void onClick(View view) {
String phoneNum = holder.contact.getText ().toString ();
Intent in = new Intent ( Intent.ACTION_DIAL );
in.setData ( Uri.parse ( "tel:" + phoneNum ) ); // This ensures only Dial apps respond
if (in.resolveActivity ( getPackageManager () ) != null) {
startActivity ( in );

} else {
Toast.makeText ( MainActivity.this , "Failed" , Toast.LENGTH_SHORT ).show ();
}
}
} );


//for longPress on particular view(CardView)
//for Delete the particular data when press long click on the View(CardView)
holder.itemView.setOnLongClickListener ( new View.OnLongClickListener () {
@Override
public boolean onLongClick(View view) {
//for post_key we simply fetch that data postion which we want to delete
post_key = getRef ( position ).getKey ();
openDeleteWindow ();//this is a method for open the Delete Window
// Toast.makeText ( MainActivity.this , "Press" , Toast.LENGTH_SHORT ).show ();
return true;
}
} );

最佳答案

对于需要在 View 中显示的每个项目,都会调用 FirebaseUI 适配器的 onBindViewHolder。如果没有与 View 中显示的引用/查询匹配的数据,则适配器的 onBindViewHolder 将永远不会被调用。

因此,如果您使用 onBindViewHolder 隐藏 progressDialog,则 progressDialog 仅在存在时才会被隐藏项目。如果没有项目,则不会调用 onBindViewHolder 并且 progressDialog 将保持可见。

要隐藏 progressDialog(无论是否有项目),您需要重写 FirebaseUI 适配器的 onDataChanged 方法并将其隐藏在那里:

adapter = new FirebaseRecyclerAdapter < modelClass, MyVIewHolder > ( options ) {
@Override
public void onDataChanged() {
progressDialog.hide();
}

@Override
protected void onBindViewHolder(@NonNull final MyVIewHolder holder , final int position , @NonNull modelClass model) {
...

由于每次从 Firebase 完全收到更新时都会调用 onDataChanged,因此当存在数据和不存在数据时都会调用它t。

另请参阅:

关于java - 为什么当 firebase 实时数据库中不存在数据时,progressDialog 不会被关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60484837/

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