如何在 Android Firebase 数据库 post 中添加更多参数?

How can I add more parameters in Android Firebase database post?

我目前正在使用 Firebase github example 创建一个用户可以注册的应用程序 post "status updates." 我能够正确地做到这一点,但现在我想添加更多参数,比如用户位置(纬度和经度),但我不确定我会把它放在哪里。我不确定在什么时候添加所有要添加的内容。这是我当前的代码:

NewPostActivity:

private DatabaseReference mDatabase;

private EditText mTextField;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_post);

    mDatabase = FirebaseDatabase.getInstance().getReference();

    mTextField = (EditText) findViewById(R.id.postText);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fabSubmitPost);
    assert fab != null;
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            submitPost();
        }
    });
}

private void submitPost() {
    final String text = mTextField.getText().toString();

    if (TextUtils.isEmpty(text)) {
        mTextField.setError("Required");
        return;
    }

    final String userId = getUid();
    mDatabase.child("users").child(userId).addListenerForSingleValueEvent new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            User user = dataSnapshot.getValue(User.class);

            if (user == null) {
                Toast.makeText(NewPostActivity.this, "Could not fetch user", Toast.LENGTH_SHORT).show();
            } else {
                writeNewPost(userId, user.username, text);
            }
            finish();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(NewPostActivity.this, databaseError.toString(), Toast.LENGTH_SHORT).show();
        }
    });
}

private void writeNewPost(String userId, String username, String text) {
    String key = mDatabase.child("posts").push().getKey();
    Post post = new Post(userId, username, text);
    Map<String, Object> postValues = post.toMap();

    Map<String, Object> childUpdates = new HashMap<>();
    childUpdates.put("/posts/" + key, postValues);
    childUpdates.put("/user-posts/" + userId + key, postValues);

    mDatabase.updateChildren(childUpdates);
}

这是 post 文件:

public class Post {

    public String uid;
    public String author;
    public String text;
    public int likeCount = 0;
    public Map<String, Boolean> likes = new HashMap<>();

    public Post() {

    }

    public Post(String uid, String author, String text) {
        this.uid = uid;
        this.author = author;
        this.text = text;
    }

    @Exclude
    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("uid", uid);
        result.put("author", author);
        result.put("text", text);
        result.put("likeCount", likeCount);
        result.put("likes", likes);

        return result;
    }
}

为了在保存 Post 时添加 Latitude & Longitude 您可以将这两个参数添加到现有的 class,您需要修改 Post class 构造函数添加 2 个参数,例如:

public double lat,lng;

public Post(String uid, String author, String text,double lat,double lng) {
    this.uid = uid;
    this.author = author;
    this.text = text;
    this.lat = lat;
    this.lng = lng;
}

然后在您的 toMap 函数中,您可以将参数添加到 result,例如:

result.put("lat", lat);
result.put("lng", lng);

因此,当您在 writeNewPost 函数中调用构造函数时,您可以更改此行:

Post post = new Post(userId, username, text);

Post post = new Post(userId, username, text, lat, lng);

现在您的新 post 将 post 连同纬度和经度

我假设你已经有了纬度和经度

更新

因为你想看到 PostsLocation 中被 post 编辑,你已经 post 编辑了它们,你可以这样做,

在您查询 postsFirebase query 中,您可以添加 orderByChildequalTo 过滤器以与当前 latitude & Longitude

如果你想根据一些距离过滤它们,那么你将不得不查询所有 posts 然后在本地比较它们