有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java根据用户输入在Firebase中更改密钥,而无需在Android中硬编码密钥值

当我试图将数据添加到firebase数据库时,我发现密钥是自动生成的。我想根据用户输入的ID值自定义键值

这是我用来将数据保存到数据库中的代码。此代码正在使用自动生成的键值:

btn6.setOnClickListener(new View.OnClickListener() { //create
        @Override
        public void onClick(View view) { //Create
            dbref = F.irebaseDatabase.getInstance().getReference().child("DetailsOfCode");
            try {
                if (TextUtils.isEmpty(description.getText().toString()))
                    Toast.makeText(getApplicationContext(), "Please enter a Description", Toast.LENGTH_SHORT).show();
                else if (TextUtils.isEmpty(code.getText().toString()))
                    Toast.makeText(getApplicationContext(), "Please enter a Code", Toast.LENGTH_SHORT).show();
                else if (TextUtils.isEmpty(id.getText().toString()))
                    Toast.makeText(getApplicationContext(), "Please enter an Id", Toast.LENGTH_SHORT).show();
                else {
                    DCode.setDescription(description.getText().toString().trim());
                    DCode.setCode(code.getText().toString().trim());
                    DCode.setId(id.getText().toString().trim());


                    dbref.push().setValue(DCode); //Here I tried to set customize Key, but always showing error

                    Toast.makeText(getApplicationContext(), "Data Saved Successfully", Toast.LENGTH_SHORT).show();
                    clearControls();
                }
            } catch (NumberFormatException e) {
                Toast.makeText(getApplicationContext(), "Invalid Data, check again", Toast.LENGTH_SHORT).show();
            }


        }
    });

我想获取键值作为“ID”用户输入。 DCode.setId(id.getText().toString().trim());<;-这是id会话,我希望此id显示为密钥

如果用户输入ID为“1”,则键值也要显示为“1”。我不想为每个数据输入硬编码键为“1”、“2”。我希望它根据ID值实现密钥

My database with autogenerated key

在这里,我想要id:1作为键:1。像这样,

- DetailsOfCode 
      - 1
         code: "cS3j2A"
         description: "Sample 1"
         id: "1"

共 (0) 个答案