如何正确声明字符串以上传到Firebase?

时间:2018-01-09 15:53:13

标签: android string firebase firebase-realtime-database

我正在尝试将应用程序连接到FireBase数据库,到目前为止,我已设法按照this example进行操作。它很好,但是当我尝试向数据库添加新字符串时,它不起作用。我猜我没有正确地声明字符串,因此我想问我在哪里做错了?

到目前为止,代码都有一个MainActivity(我写了//Here作为注释,我添加了关于字符串的信息):

public class MainActivity extends AppCompatActivity {

  private RecyclerView mPostRV;
  private FirebaseRecyclerAdapter < Post, PostViewHolder > mPostAdapter;
  private DatabaseReference mPostRef;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    initialiseScreen();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        sendPostToFirebase();
      }
    });
  }

  private void sendPostToFirebase() {
    Post post = new Post();
    String UID = Utils.getUID();

    post.setHeadline("Headline"); //HERE
    post.setUID(UID);
    post.setNumLikes(0);
    post.setImageUrl("gs://something_something.jpg");
    mPostRef.child(UID).setValue(post);
  }

  private void initialiseScreen() {
    mPostRV = (RecyclerView) findViewById(R.id.post_rv);
    mPostRV.setLayoutManager(new LinearLayoutManager(MainActivity.this));
    mPostRef = FirebaseDatabase.getInstance().getReference(Constants.POSTS);
    setupAdaptater();
    mPostRV.setAdapter(mPostAdapter);
  }

  private void setupAdaptater() {
    mPostAdapter = new FirebaseRecyclerAdapter < Post, PostViewHolder > (
      Post.class,
      R.layout.item_layout_post,
      PostViewHolder.class,
      mPostRef
    ) {
      @Override
      protected void populateViewHolder(PostViewHolder viewHolder, final Post model, int position) {
        StorageReference storageReference = FirebaseStorage.getInstance().getReferenceFromUrl(model.getImageUrl());
        Glide.with(MainActivity.this)
          .using(new FirebaseImageLoader())
          .load(storageReference)
          .into(viewHolder.postIV);

        viewHolder.setNumLikes(model.getNumLikes());
        viewHolder.postLikeIV.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            updateNumLikes(model.getUID());
          }
        });
      }
    };
  }

  private void updateNumLikes(String uid) {
    mPostRef.child(uid).child(Constants.NUM_LIKES)
      .runTransaction(new Transaction.Handler() {
        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
          long num = (long) mutableData.getValue();
          num++;
          mutableData.setValue(num);
          return Transaction.success(mutableData);
        }

        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {

        }
      });
  }

  public static class PostViewHolder extends RecyclerView.ViewHolder {
    public ImageView postIV;
    public ImageView postLikeIV;
    public TextView numLikesIV;
    public TextView postHeadline; //HERE

    public PostViewHolder(View itemView) {
      super(itemView);
      postIV = (ImageView) itemView.findViewById(R.id.post_iv);
      postLikeIV = (ImageView) itemView.findViewById(R.id.like_iv);
      numLikesIV = (TextView) itemView.findViewById(R.id.num_likes_tv);
      postHeadline = (TextView) itemView.findViewById(R.id.headline_event); //HERE
    }

    public void setPostImage(String url) {
      StorageReference storageReference = FirebaseStorage.getInstance().getReferenceFromUrl(url);
    }

    public void setHeadline(String headline) { //HERE
      postHeadline.setText(String.valueOf(headline)); //HERE
    }

    public void setNumLikes(long num) {
      numLikesIV.setText(String.valueOf(num));
    }
  }

}

一个名为Post的课程

public class Post {
  private String imageUrl;
  private long numLikes;
  private String nameHeadline; //Here
  private String UID;


  public Post() {}

  public Post(String imageUrl, long numLikes, String UID, String nameHeadline) {
    this.imageUrl = imageUrl;
    this.numLikes = numLikes;
    this.UID = UID;
    this.nameHeadline = nameHeadline; //Here
  }

  public String getUID() {
    return UID;
  }

  public void setUID(String UID) {
    this.UID = UID;
  }

  public String getImageUrl() {
    return imageUrl;
  }

  public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
  }

  public long getNumLikes() {
    return numLikes;
  }

  public void setNumLikes(long numLikes) {
    this.numLikes = numLikes;
  }

  public void setHeadline(String headline) { //Here
    this.nameHeadline = headline; //Here
  }


}

代码运行完美,但它不会在数据库上写入,也不会读取有关该代码的任何信息。任何人都可以帮我这个吗?

2 个答案:

答案 0 :(得分:1)

由于您的代码未收到任何错误,请检查您的Firebase数据库规则是否已正确设置。例如,如果您希望任何人从您的数据库中读取和写入,请将规则设置为以下内容:

// These rules give anyone, even people who are not users of your app,
// read and write access to your database

{
  "rules": {
    ".read": true,
    ".write": true
 }
}

但是,如果您设置了身份验证,则需要专门为此设置规则。请查看此链接以获取更多信息。 https://firebase.google.com/docs/database/security/quickstart

答案 1 :(得分:1)

您添加了一个私有字符串。它应该有公共getter和setter,以便Firebase可以访问它。我看到你有一个setter,但方法名称与变量名称不匹配。您应该将其重命名为:

public void setNameHeadline(String nameHeadline) {
    this.nameHeadline = nameHeadline;
  }

//and add a getter:
public String getNameHeadline(){
    return nameHeadline;
}