Firebase:输入值时,我希望按顺序保存密钥

时间:2018-11-22 05:28:04

标签: java android firebase firebase-realtime-database

我现在要输入键和值并将其保存在Firebase中。 但是我希望在输入Value时按顺序保存键。

例如,如果一个密钥最多存储3个,则下一个值应为数字4。

enter image description here

package com.dogeun.auth;

import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.FirebaseDatabase;


public class ShootingActivity extends AppCompatActivity {

private Button insert_button;
private EditText Edit_Num;
private EditText Edit_Text;

String num;
String text;

private FirebaseAuth mFirebaseAuth;
private FirebaseUser mFirebaseUser;
private FirebaseDatabase mFirebaseDatabase;


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

    mFirebaseAuth = mFirebaseAuth.getInstance();
    mFirebaseUser = mFirebaseAuth.getCurrentUser();
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    insert_button = (Button) findViewById(R.id.insert_button);
    Edit_Num = (EditText) findViewById(R.id.edit_Num);
    Edit_Text = (EditText) findViewById(R.id.edit_Text);

    insert_button.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            saveText();
        }
    });
}

当前,键是直接通过EditText输入的。

Edit_Num =键

Edit_Text =值

private void saveText() {
    String Text1 = Edit_Text.getText().toString();
    if(Text1.isEmpty()){
        return;
    }
    num = Edit_Num.getText().toString();
    text = Edit_Text.getText().toString();
    mFirebaseDatabase.getReference("Book/"+mFirebaseUser.getUid())
            .child(num)
            .setValue(text)
            .addOnSuccessListener(ShootingActivity.this, new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Snackbar.make(Edit_Text,"text insert.", Snackbar.LENGTH_LONG).show();
                    Edit_Num.setText("");
                    Edit_Text.setText("");
                }
            });
}
}

..

package com.dogeun.auth;

import com.google.firebase.database.Exclude;

import java.util.HashMap;
import java.util.Map;

public class Book {

public String key;
public String num;
public String text;

public Book(String key, String num, String text) {
    this.key = key;
    this.num = num;
    this.text = text;
}

public String getNum() {
    return num;
}

public void setNum(String num) {
    this.num = num;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public String getKey() {
    return key;
}

public void setKey(String key) {
    this.key = key;
}
}

请帮助我~~

0 个答案:

没有答案