我有一个从Firebase检索数据的活动,并创建了一个帖子。但是,如果将其从最近的应用程序中删除或停止,则必须重新加载它。我想将其永久存储在应用程序中。我该怎么办?
我的活动:
public class MainActivity extends AppCompatActivity {
private RecyclerView mbloglist;
private DatabaseReference mdatabase;
public static final String TAG = "Homeactivity";
private Context mcontext = MainActivity.this;
private static final int ACTIVITY_NUM = 0;
Dialog mycustomDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: starting");
mdatabase = FirebaseDatabase.getInstance().getReference().child("Global");
mdatabase.keepSynced(true);
mbloglist = (RecyclerView)findViewById(R.id.recycleView_post);
mbloglist.setHasFixedSize(true);
mbloglist.setLayoutManager(new LinearLayoutManager(this));
setupBottomNavigationView();
setupToolbar();
mycustomDialog = new Dialog(this);
}
@Override
protected void onStart() {
super.onStart();
final ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar_post);
progressBar.setVisibility(View.VISIBLE);
FirebaseRecyclerAdapter<Blog,BlogViewHolder>firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,R.layout.layout_post,BlogViewHolder.class,mdatabase
) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDesc());
viewHolder.setImage(getApplicationContext(),model.getImage());
viewHolder.setTime(model.getTime());
}
};
mdatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
progressBar.setVisibility(View.GONE);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, "onCancelled: event cancelled");
}
});
mbloglist.setAdapter(firebaseRecyclerAdapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder {
View mview;
public BlogViewHolder(View itemView) {
super(itemView);
mview = itemView;
}
public void setTitle(String title) {
TextView post_title = (TextView) itemView.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setDesc(String desc) {
TextView post_desc = (TextView) itemView.findViewById(R.id.post_description);
post_desc.setText(desc);
}
public void setImage(Context context,String image) {
PhotoView post_image = (PhotoView) itemView.findViewById(R.id.post_image);
Glide.with(context).asBitmap().load(image).into(post_image);
}
public void setTime(String time) {
TextView post_desc = (TextView) itemView.findViewById(R.id.post_time);
post_desc.setText(time);
}
}
即使没有互联网连接,我也可以保存并显示此帖子。并在连接可用时重新加载帖子。
答案 0 :(得分:0)
也可以使用
DatabaseReference db = FirebaseDatabase.getInstance();
db.setPersistenceEnabled(true);
mDatabase = db.getReference().child("Global");;
访问到docs-
通过启用持久性,即使用户或操作系统重新启动应用程序,Firebase Realtime Database客户端在联机时将同步的任何数据也将持久保存到磁盘,并且可以脱机使用。这意味着您的应用程序将通过使用存储在缓存中的本地数据来像在线操作一样工作。
以及
mdatabase.keepSynced(true);