从Firebase实时数据库加载数据时,我想添加一个进度栏。我不知道在加载数据时添加进度条。我在下面提供了活动代码。谁能帮助我做到这一点?
任何帮助将不胜感激。谢谢............................
package com.example.bhaskargogoi.questionpaper;
import ...
public class View_PDF_Files extends AppCompatActivity {
ListView myPDFListView;
DatabaseReference databaseReference;
List<uploadPDF> uploadPDFS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view__pdf__files);
getSupportActionBar().setTitle("View Question Papers");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
myPDFListView = (ListView)findViewById(R.id.myListView);
uploadPDFS = new ArrayList<>();
viewAllFiles();
myPDFListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
uploadPDF uploadPDF = uploadPDFS.get(position);
Intent intent = new Intent();
intent.setType(Intent.ACTION_VIEW);
intent.setData(Uri.parse(uploadPDF.getUrl()));
startActivity(intent);
}
});
}
private void viewAllFiles() {
databaseReference = FirebaseDatabase.getInstance().getReference("uploads");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
uploadPDF uploadPDF = postSnapshot.getValue(uploadPDF.class);
uploadPDFS.add(uploadPDF);
}
String[] uploads = new String[uploadPDFS.size()];
for (int i = 0; i<uploads.length; i++) {
uploads[i] = uploadPDFS.get(i).getName();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,uploads){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView myText = (TextView) view.findViewById(android.R.id.text1);
myText.setTextColor(Color.BLACK);
return view;
}
};
myPDFListView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
答案 0 :(得分:1)
查看并更新您喜欢的代码;
//Show Progress Dialog\\
File localFile = File.createTempFile("image", "jpg");
yourStorageReference.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
//Dismiss Progress Dialog\\
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//Dismiss Progress Dialog\\
}
}).addOnProgressListener(new OnProgressListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onProgress(FileDownloadTask.TaskSnapshot taskSnapshot) {
//calculating progress percentage
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
//displaying percentage in progress dialog
yourProgressDialog.setMessage("Uploaded " + ((int) progress) + "%...");
}
});