I use Firebase to display data in text form. I use asynctask to load data in the background, when I download, I want to show the loaded message before the data is loaded into my text view.
But the popup data loading window is not displayed. (i.e., the doinBackgroundmethod exits immediately), and the user sees an empty text view and only after the Firebase data is loaded once.
How to prevent this and show a process dialog until my text image is loaded with Firebase data?
here is my code
<pre>
<code>
public class LastPage extends AppCompatActivity {
TextView title, author, article;
DatabaseReference mDatabase;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last_page);
title = (TextView) findViewById(R.id.last_page_title);
author = (TextView) findViewById(R.id.last_page_author);
article = (TextView) findViewById(R.id.last_page_article);
mDatabase = FirebaseDatabase.getInstance().getReference();
new LoadFirebaseData().execute();
}
private class LoadFirebaseData extends AsyncTask<Void, Void, Integer>
{
private ProgressDialog Dialog = new ProgressDialog(LastPage.this);
@Override
protected void onPreExecute()
{
try {
Dialog.setMessage("Doing something...");
Dialog.show();
}catch (Exception e)
{
e.printStackTrace();
}
}
@Override
protected void onPostExecute(Integer result)
{
if(result==0)
{
}
Dialog.dismiss();
}
@Override
protected Integer doInBackground(Void... params) {
mDatabase.child("version_1_5").child("title").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String temp = dataSnapshot.getValue(String.class);
title.setText(temp);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mDatabase.child("version_1_5").child("author").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String temp = dataSnapshot.getValue(String.class);
author.setText(temp);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mDatabase.child("version_1_5").child("article").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String temp = dataSnapshot.getValue(String.class);
article.setText(temp);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return 0;
}
}
}
</code>
</pre>
JSON tree structure:
Root
Version_1_5
Title:TitleValue
Author:AuthorValue
Article:ArticleValue