从 firebase 数据库获取数据时显示进度对话框
Show Progress dialog while fetching data from firebase database
我希望 Activity 在从 Firebase 获取数据时必须显示进度对话框 database.it 未显示任何内容并崩溃。下面是我的代码
public class profilemain extends AppCompatActivity {
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private ProgressDialog progressDialog;
DatabaseReference mref =
FirebaseDatabase.getInstance().getReference("users");
ListView mlistview;
ArrayList<String> arrayList=new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
private BottomNavigationView.OnNavigationItemSelectedListener
mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
progressDialog.show();
startActivity(new
Intent(profilemain.this,profilemain.class));
return true;
case R.id.navigation_dashboard:
Toast.makeText(profilemain.this,"hi
hello",Toast.LENGTH_SHORT).show();
return true;
case R.id.navigation_notifications:
Toast.makeText(profilemain.this,"hi
hello",Toast.LENGTH_SHORT).show();
return true;
case R.id.navigation_signout:
Toast.makeText(profilemain.this,"You have successfully
Signed out",Toast.LENGTH_SHORT).show();
mAuth.signOut();
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profilemain);
progressDialog.setMessage("loading");
progressDialog.setTitle("database is");
mlistview=(ListView) findViewById(R.id.listview);
Firebase.setAndroidContext(this);
DatabaseReference ref = FirebaseDatabase.getInstance()
.getReference("users");
//mref=new Firebase("https://stark-1dffd.firebaseio.com/users");
arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
mlistview.setAdapter(arrayAdapter);
mref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value= dataSnapshot.getValue(String.class);
arrayList.add(value);
arrayAdapter.notifyDataSetChanged();
//
//
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser()==null){
startActivity(new
Intent(profilemain.this,MainActivity.class));
}
}
};
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
Process: com.food.sheenishere.stark, PID: 16408
com.google.firebase.database.DatabaseException: Failed to convert value of type
java.util.HashMap to String
at com.google.android.gms.internal.zg.zzb(Unknown Source)
at com.google.android.gms.internal.zg.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.food.sheenishere.stark.home.onChildAdded(home.java:49)
at com.google.android.gms.internal.px.zza(Unknown Source)
at com.google.android.gms.internal.vj.zzHX(Unknown Source)
at com.google.android.gms.internal.vp.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
07-15 20:49:51.645 16408-16435/com.food.sheenishere.stark W/DynamiteModule:
Local module descriptor class for com.google.firebase.auth not found.
您的代码中的问题是您正试图在 ProgressDialog
对象上使用 show()、setMessage("loading") 和 setTitle("database is") 方法,而这些方法从未已初始化。
为了解决您的问题,您需要使用以下代码:
ProgressDialog progressDialog = new ProgressDialog(this);
一定会解决你的问题。
您应该初始化进度对话框..如下所示
private ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
pDialog = new ProgressDialog(profilemain.this);
pDialog.setMessage("loading..");
}
我希望 Activity 在从 Firebase 获取数据时必须显示进度对话框 database.it 未显示任何内容并崩溃。下面是我的代码
public class profilemain extends AppCompatActivity {
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private ProgressDialog progressDialog;
DatabaseReference mref =
FirebaseDatabase.getInstance().getReference("users");
ListView mlistview;
ArrayList<String> arrayList=new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
private BottomNavigationView.OnNavigationItemSelectedListener
mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
progressDialog.show();
startActivity(new
Intent(profilemain.this,profilemain.class));
return true;
case R.id.navigation_dashboard:
Toast.makeText(profilemain.this,"hi
hello",Toast.LENGTH_SHORT).show();
return true;
case R.id.navigation_notifications:
Toast.makeText(profilemain.this,"hi
hello",Toast.LENGTH_SHORT).show();
return true;
case R.id.navigation_signout:
Toast.makeText(profilemain.this,"You have successfully
Signed out",Toast.LENGTH_SHORT).show();
mAuth.signOut();
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profilemain);
progressDialog.setMessage("loading");
progressDialog.setTitle("database is");
mlistview=(ListView) findViewById(R.id.listview);
Firebase.setAndroidContext(this);
DatabaseReference ref = FirebaseDatabase.getInstance()
.getReference("users");
//mref=new Firebase("https://stark-1dffd.firebaseio.com/users");
arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
mlistview.setAdapter(arrayAdapter);
mref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value= dataSnapshot.getValue(String.class);
arrayList.add(value);
arrayAdapter.notifyDataSetChanged();
//
//
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser()==null){
startActivity(new
Intent(profilemain.this,MainActivity.class));
}
}
};
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
Process: com.food.sheenishere.stark, PID: 16408
com.google.firebase.database.DatabaseException: Failed to convert value of type
java.util.HashMap to String
at com.google.android.gms.internal.zg.zzb(Unknown Source)
at com.google.android.gms.internal.zg.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.food.sheenishere.stark.home.onChildAdded(home.java:49)
at com.google.android.gms.internal.px.zza(Unknown Source)
at com.google.android.gms.internal.vj.zzHX(Unknown Source)
at com.google.android.gms.internal.vp.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
07-15 20:49:51.645 16408-16435/com.food.sheenishere.stark W/DynamiteModule:
Local module descriptor class for com.google.firebase.auth not found.
您的代码中的问题是您正试图在 ProgressDialog
对象上使用 show()、setMessage("loading") 和 setTitle("database is") 方法,而这些方法从未已初始化。
为了解决您的问题,您需要使用以下代码:
ProgressDialog progressDialog = new ProgressDialog(this);
一定会解决你的问题。
您应该初始化进度对话框..如下所示
private ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
pDialog = new ProgressDialog(profilemain.this);
pDialog.setMessage("loading..");
}