How to store posts created by FirebaseUi in the app?

限于喜欢 提交于 2019-12-11 15:18:16

问题


I have an Activity which retrieves data from Firebase and a post is created. But it has to get reloaded if it is removed from recent app or stopped. I want to store this permanently in the app. How can I do that?

My Activity:

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);
        }
    }

can I have this post be saved and shown even if their is no internet connection. and reload the post when connection is made available.


回答1:


Also Use

 DatabaseReference db = FirebaseDatabase.getInstance();                                        
 db.setPersistenceEnabled(true);
 mDatabase = db.getReference().child("Global");;

Acc. to docs -

By enabling persistence, any data that the Firebase Realtime Database client would sync while online persists to disk and is available offline, even when the user or operating system restarts the app. This means your app works as it would online by using the local data stored in the cache.

along with

mdatabase.keepSynced(true);


来源:https://stackoverflow.com/questions/51686808/how-to-store-posts-created-by-firebaseui-in-the-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!