我无法从Firebase检索数据

时间:2019-03-07 20:41:22

标签: android

我刚刚开始在Firebase上学习,我将使用它编写一个简单的应用程序。

在这个简单的应用程序中,我想做的是允许创建帖子,然后列出并查看它们。

该应用现在可以创建帖子。

但是我不能在主要活动中显示该帖子。

任何人都可以帮忙吗?

以下是我的代码。

    package com.example.rami_.simpleblog2019;

    import android.content.Intent;
    import android.support.annotation.NonNull;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    import com.firebase.ui.database.FirebaseListOptions;
    import com.firebase.ui.database.FirebaseRecyclerAdapter;
    import com.firebase.ui.database.FirebaseRecyclerOptions;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;
    import com.google.firebase.database.Query;


    public class MainActivity extends AppCompatActivity {
        private RecyclerView mBlogList;
        private DatabaseReference mDataBase;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mDataBase = FirebaseDatabase.getInstance().getReference().child("Blog");
            mBlogList = (RecyclerView) findViewById(R.id.blog_list);
            mBlogList.setHasFixedSize(true);
            mBlogList.setLayoutManager(new LinearLayoutManager(this));
        }


        @Override
        protected void onStart() {
            super.onStart();
            Query query = FirebaseDatabase.getInstance()
                    .getReference()
                    .child("Blog");

            FirebaseRecyclerOptions<Blog> options =
                    new FirebaseRecyclerOptions.Builder<Blog>()
                            .setQuery(query, Blog.class)
                            .build();

            FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter =  new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull BlogViewHolder holder, int position, @NonNull Blog model) {
                    holder.setTitle(model.getTitle());
                    holder.setDesc(model.getDesc());
                }

                @NonNull
                @Override
                public BlogViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                    View view = LayoutInflater.from(viewGroup.getContext())
                            .inflate(R.layout.blog_row, viewGroup, false);

                    return new BlogViewHolder(view);
                }
            };
            mBlogList.setAdapter(firebaseRecyclerAdapter);
        }


        public static class BlogViewHolder extends RecyclerView.ViewHolder{
            View mView;
            public BlogViewHolder(@NonNull View itemView) {
                super(itemView);
                mView = itemView;
            }

            public void setTitle(String title)
            {
                TextView post_title = (TextView) mView.findViewById(R.id.post_title);
                post_title.setText(title);
            }

            public void setDesc(String desc)
            {
                TextView post_desc = (TextView) mView.findViewById(R.id.post_desc);
                post_desc.setText(desc);
            }

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main_menu, menu);
            return super.onCreateOptionsMenu(menu);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (item.getItemId() == R.id.action_add)
            {
                startActivity(new Intent(MainActivity.this, PostActivity.class));
            }
            return super.onOptionsItemSelected(item);
        }
    }



0 个答案:

没有答案