我从Firebase数据库加载ListView,我希望在加载ListView时,它会显示进度条。
我已经阅读了一些指南,但我尝试了一些事情,但它对我没用。
这是我的代码:
public class Event_List_Activity extends Activity {
private static final String FIREBASE_URL="https://****.firebaseio.com/";
private Firebase firebaseRef;
final ProgressBar pb=(ProgressBar)findViewById(R.id.progressBar);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_event__list_);
Firebase.setAndroidContext(this);
firebaseRef = new Firebase(FIREBASE_URL);
//populateEventList();
new MyTask().execute();
final ListView listView = (ListView)findViewById(R.id.listView1);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
openEventInfo(parent,view,position,id);
}
});
}
class MyTask extends AsyncTask<String,Integer,String>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected String doInBackground(String...strings)
{
populateEventList();
return null;
}
@Override
protected void onProgressUpdate(Integer...values)
{
super.onProgressUpdate(values);
pb.setProgress(values[0]);
}
@Override
protected void onPostExecute(String s)
{
super.onPostExecute(s);
}
}
private void populateEventList()
{
firebaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
ArrayList<String> subTitles = new ArrayList<String>();
ArrayList<EventClass> eventList = new ArrayList<EventClass>();
ArrayList<String> titles = new ArrayList<String>();
for (DataSnapshot user : snapshot.child("users").getChildren()) {
String event_title = user.child("event/event_title").getValue().toString();
String event_date = user.child("event/event_date").getValue().toString();
subTitles.add(event_date);
String event_content = user.child("event/event_content").getValue().toString();
String age_limit = user.child("event/age_limit").getValue().toString();
String event_hour = user.child("event/event_hour").getValue().toString();
String location_left = user.child("location_left").getValue().toString();
String location_right = user.child("location_right").getValue().toString();
EventClass oneEvent = new EventClass(event_title, event_date, event_content, age_limit, event_hour, location_left, location_right);
eventList.add(oneEvent);
}
for (EventClass event : eventList) {
titles.add(event.getEvent_title());
}
ArrayAdapter adapter = new ArrayAdapter(Event_List_Activity.this, android.R.layout.simple_list_item_1, titles) {
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setTextColor(Color.WHITE);
return view;
}
};
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
String message = "Error";
Toast.makeText(Event_List_Activity.this, message, Toast.LENGTH_SHORT).show();
}
});
}
private void openEventInfo(AdapterView<?> parent, View view, int position, long id)
{
final String selectedTitle = parent.getItemAtPosition(position).toString();
firebaseRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot user : dataSnapshot.child("users").getChildren()) {
String event_title = user.child("event/event_title").getValue().toString();
if (selectedTitle.equals(event_title)) {
String event_date = user.child("event/event_date").getValue().toString();
String event_content = user.child("event/event_content").getValue().toString();
String age_limit = user.child("event/age_limit").getValue().toString();
String event_hour = user.child("event/event_hour").getValue().toString();
String location_left = user.child("location_left").getValue().toString();
String location_right = user.child("location_right").getValue().toString();
final SharedPreferences sp = getSharedPreferences("key", 0);
final SharedPreferences.Editor sedt = sp.edit();
sedt.putString("event_title", event_title);
sedt.putString("event_date", event_date);
sedt.putString("event_content", event_content);
sedt.putString("age_limit", age_limit);
sedt.putString("event_hour", event_hour);
sedt.putString("location_left", location_left);
sedt.putString("location_right", location_right);
sedt.commit();
}
}
Intent intent = new Intent(Event_List_Activity.this, EventInfo.class);
startActivity(intent);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Toast.makeText(Event_List_Activity.this, "Error", Toast.LENGTH_LONG).show();
}
});
}
}
阅读评论后,这是我在MyTask()
方法中所做的更改:
class MyTask extends AsyncTask<String,Integer,String>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
listView.setVisibility(View.INVISIBLE);
pb.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(String...strings)
{
populateEventList();
return null;
}
@Override
protected void onProgressUpdate(Integer...values)
{
super.onProgressUpdate(values);
pb.setProgress(values[0]);
pb.setMax(values[1]);
}
@Override
protected void onPostExecute(String s)
{
super.onPostExecute(s);
pb.setVisibility(View.GONE);
listView.setVisibility(View.VISIBLE);
}
}
答案 0 :(得分:0)
好FireBase
为每项任务提供完整的解决方案。以下是我们在下载或上传内容时如何显示进度的方法。如this blog所述,我们可以使用FireBase
API的内置方法。
首先,请删除所有AsyncTask代码,以便下载并显示进度。 FireBase已经在其他线程上工作,因此不需要AsyncTask。所以你做了类似的事情
StorageReference storageRef = FirebaseStorage.getInstance().reference().child("folderName/file.jpg");
Uri file = Uri.fromFile(new File("path/to/images/file.jpg"));
UploadTask uploadTask = storageRef.putFile(file);
uploadTask.addOnProgressListener(new OnProgressListener() {
@Override
public void onProgress(UploadTask.TaskSnapshot snapshot) {
System.out.println(snapshot.getBytesTransferred().toString());
}
});
让我知道它是否对你有帮助。
答案 1 :(得分:-1)
您必须在某处publishProgress
手动拨打doInBackground
AsyncTask publishProgress
编辑:
让我更具体一点...... 如果您只想在加载数据时显示进度条,则可以执行类似的操作。
在onPreExecute
中,您可以隐藏listView
并将visibility
的{{1}}设置为progressBar
。
然后在VIEW.VISIBLE
每次收到新数据集时,您可以使用当前接收数据数和最大数量来调用doInBackground
。
然后在publishProgress(current, max)
中,您按照onProgressUpdate(Integer... values)
和实际值pb.setMax(values[1])
设置新的最大值。
加载每个数据集后,pb.setProgress(values[0])
将被调用,您将onPostExecute()
的可见性更改回progressBar
,并将VIEW.GONE
的可见性设置回{{ 1}}。