如何通过startListening()解决错误,同时在Firebase数据获取时显示进度条?



这些是我的代码,用于在将数据从Firebase获取到回收视图时显示进度条。但是 startListening(( 在 onStart(( {...} 中显示错误。我不知道放置这行代码的确切位置。我已经在MyTask((类中尝试过这个,但它使应用程序崩溃。请检查我的代码并帮助我解决此问题。谢谢

public class Tab1 extends android.support.v4.app.Fragment {
private RecyclerView mPeopleRV;
private DatabaseReference mDatabase;
private FirebaseRecyclerAdapter<News, Tab1.NewsViewHolder> mPeopleRVAdapter;
ProgressBar pb;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab1, container, false);

pb = view.findViewById(R.id.pb);
mPeopleRV = (RecyclerView) view.findViewById(R.id.myRecycleView_tab1);
mPeopleRV.hasFixedSize();
mPeopleRV.setLayoutManager(new LinearLayoutManager(this.getActivity()));

new MyTask().execute();

return view;
}
@Override
public void onStart() {
super.onStart();
mPeopleRVAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();

}

class MyTask extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... voids) {

pb.setVisibility(View.VISIBLE);
// FirebaseCode

mDatabase = FirebaseDatabase.getInstance().getReference().child("Cricket");
mDatabase.keepSynced(true);
DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference().child("Cricket");
Query personsQuery = personsRef.orderByKey();

FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<News>().setQuery(personsQuery, News.class).build();
mPeopleRVAdapter = new FirebaseRecyclerAdapter<News, NewsViewHolder>(personsOptions) {
@Override
protected void onBindViewHolder(NewsViewHolder holder, final int position, final News model) {
holder.setTitle(model.getTitle());
holder.setDesc(model.getDesc());
holder.setImage1(getActivity().getBaseContext(), model.getImage1());
holder.setImage2(getActivity().getBaseContext(), model.getImage2());
holder.setT1(model.getT1());
holder.setT2(model.getT2());
holder.setStatus_r(model.getStatus_r());
holder.setStatus_g(model.getStatus_g());

holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String url = model.getUrl();
final String offers = model.getTarget();
if (url == null) {
Snackbar snackbar = Snackbar.make(getActivity().findViewById(android.R.id.content), "Will be available soon", Snackbar.LENGTH_LONG);
View sbView = snackbar.getView();
sbView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
snackbar.show();
} else {
if (offers == null) {
Intent intent = new Intent(getActivity(), WebViewActivity.class);
intent.putExtra("id", url);
startActivity(intent);

} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}

}
}
});
}
@Override
public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row1, parent, false);
return new NewsViewHolder(view);
}
};

mPeopleRV.setAdapter(mPeopleRVAdapter);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
pb.setVisibility(View.GONE);
}
@Override
protected void onCancelled() {
mPeopleRVAdapter.stopListening();
}
}


public static class NewsViewHolder extends RecyclerView.ViewHolder {
View mView;
public NewsViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title) {
TextView post_title = (TextView) mView.findViewById(R.id.post_title1);
post_title.setText(title);
}
public void setDesc(String desc) {
TextView post_desc = (TextView) mView.findViewById(R.id.post_desc1);
post_desc.setText(desc);
}
public void setImage1(Context ctx, String image1) {
ImageView post_image = (ImageView) mView.findViewById(R.id.post_image1);
Picasso.with(ctx).load(image1).into(post_image);
}
public void setImage2(Context ctx, String image2) {
ImageView post_image = (ImageView) mView.findViewById(R.id.post_image2);
Picasso.with(ctx).load(image2).into(post_image);
}
public void setT1(String t1) {
TextView post_t1 = (TextView) mView.findViewById(R.id.team1_name);
post_t1.setText(t1);
}
public void setT2(String t2) {
TextView post_t2 = (TextView) mView.findViewById(R.id.team2_name);
post_t2.setText(t2);
}
public void setStatus_r(String status_r) {
TextView status = (TextView) mView.findViewById(R.id.status_upcoming1);
status.setText(status_r);
}
public void setStatus_g(String status_g) {
TextView status = (TextView) mView.findViewById(R.id.status_available1);
status.setText(status_g);
}
}
}

首先,在 xml 中创建一个进度条。

<ProgressBar
android:id="@+id/progressBar1"
android:layout_marginTop="30dp"
android:visibility="gone"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_width="50dp"
android:layout_height="50dp"
/>

然后,在 AsyncTask 中:

@Override
protected String doInBackground(String... params) {
progressBar.setVisibility(View.VISIBLE);
// FirebaseCode

return "this string is passed to onPostExecute";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressBar.Visibility(View.GONE);
}

相关内容

  • 没有找到相关文章

最新更新