【问题标题】:adding new collections to same document using firestore in android studio在 android studio 中使用 firestore 将新集合添加到同一文档
【发布时间】:2019-04-28 05:26:06
【问题描述】:

我正在开发 Android 应用程序,当我要求某人注册时,他们可以是 userprovider。然后它会询问诸如fname,lname,address 之类的东西。目前,我将新的providers 存储在demoProviders 文档中。我的代码现在的方式是,每当我创建添加新的provider 时,存储的任何数据都会被新的provider 重写。这意味着我一次只能有一个提供者。我想在每次有人注册时创建新的providers,并将其添加到相同的document,但作为新的collection。我是firestore 的新手,因此我们将不胜感激。我将在下面发布我的UserProfileActivity

更新:添加了firestore 结构的图像

ProviderSignUp

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.android.gms.dynamic.ObjectWrapper;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.FirebaseFirestore;

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

public class ProviderSignUp extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private static final String TAG = "MainActivity";
    private static final String KEY_FNAME = "First Name";
    private static final String KEY_LNAME = "Last Name";
    private static final String KEY_ADDRESS = "Address";
    private static final String KEY_SPINNER_VALUE = "Spinner Value";
    private static final String KEY_FIXED= "Fixed Rate";
    private static final String KEY_HOURLY = "Hourly Rate";
    private static final String KEY_AGE_VALE= "Age Value";
    private static final String KEY_DOLLAR_VALUE = "Dollar Value";
    private static final String KEY_YES_INSURED = "Insured";
    private static final String KEY_NO_INSURED = "Insured";


    private EditText fName;
    private EditText lName;
    private EditText address;
    private Spinner my_spinner;
    private RadioButton fixedRadioButton;
    private RadioButton hourlyRadioButton;
    private EditText ageEditText;
    private EditText dollarEditText;
    private RadioButton yesButton;
    private RadioButton noButton;
    private RadioGroup daGroup;
    private RadioGroup daGroup2;


    // Reference to firestore database
    private FirebaseFirestore db = FirebaseFirestore.getInstance();


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


        Spinner spinner = findViewById(R.id.mySpinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.provider_choices, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

        // FireStore Storage for Provider

        fName = findViewById(R.id.firstName);
        lName = findViewById(R.id.lastName);
        address = findViewById(R.id.Address);
        ageEditText = findViewById(R.id.ageEditText);
        dollarEditText = findViewById(R.id.dollarEditText);

        my_spinner = (Spinner)findViewById(R.id.mySpinner);
        fixedRadioButton = findViewById(R.id.fixedRadioButton);
        hourlyRadioButton = findViewById(R.id.hourlyRadioButton);

        yesButton = findViewById(R.id.yesRadioButton);
        noButton = findViewById(R.id.noRadioButton);
        daGroup = findViewById(R.id.myRadioGroup);
        daGroup2 = findViewById(R.id.hourlyFixedRadioGroup);
    }

    @Override
    public void onBackPressed() {
        startActivity(new Intent(ProviderSignUp.this,PreSignUp.class));
        finish();
    }

    // These two methods below are for the spinner in the ProviderSignUp
    @Override
    // Will show a toast message after user selects spinner item
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
        String text = adapterView.getItemAtPosition(position).toString();
        Toast.makeText(adapterView.getContext(), text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

    // This method will save into firestore
    public void saveInfo(View view){

        String fname = fName.getText().toString();
        String lname = lName.getText().toString();
        String my_address = address.getText().toString();
        String spinner = my_spinner.getSelectedItem().toString();
        String fixed_radioButton = fixedRadioButton.getText().toString();
        String hourly_Radiobutton = hourlyRadioButton.getText().toString();
        String age = ageEditText.getText().toString();
        String dollar = dollarEditText.getText().toString();
        String yes_button = yesButton.getText().toString();
        String no_button = noButton.getText().toString();

        // checks field if empty or not
        if(isEmptyField(fName))return;
        if(isEmptyField(lName))return;
        if(isEmptyField(address))return;
        if(isEmptyField(ageEditText))return;
        if(isEmptyField(dollarEditText))return;


        Map<String,Object> myMap = new HashMap<String,Object>();
        myMap.put(KEY_FNAME,fname);
        myMap.put(KEY_LNAME,lname);
        myMap.put(KEY_ADDRESS, my_address);
        myMap.put(KEY_AGE_VALE, age);
        myMap.put(KEY_DOLLAR_VALUE, dollar);
        myMap.put(KEY_SPINNER_VALUE , spinner);

        if(daGroup2.getCheckedRadioButtonId() == R.id.hourlyRadioButton){
            myMap.put(KEY_HOURLY, true);
        }else if(daGroup2.getCheckedRadioButtonId() == R.id.fixedRadioButton){
            myMap.put(KEY_FIXED , true );
        }

        if (daGroup.getCheckedRadioButtonId() == R.id.yesRadioButton){
            myMap.put(KEY_YES_INSURED, true);
        } else if (daGroup.getCheckedRadioButtonId() == R.id.noRadioButton){
            myMap.put(KEY_NO_INSURED, false);
        }


        db.collection("demoProviders").document("First Provider")
                .set(myMap).addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                Toast.makeText(ProviderSignUp.this, "User Saved", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(ProviderSignUp.this,MainActivity.class));

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(ProviderSignUp.this, "Error!", Toast.LENGTH_SHORT).show();
                Log.d(TAG, e.toString());
            }
        });
    }

    // This method will validate if EditText fields are empty
    private boolean isEmptyField (EditText editText){
        boolean result = editText.getText().toString().length() <= 0;
        if (result)
            Toast.makeText(ProviderSignUp.this, "Fill all fields!", Toast.LENGTH_SHORT).show();
        return result;
    }
}

【问题讨论】:

    标签: java firebase android-studio google-cloud-firestore


    【解决方案1】:

    要解决这个问题,请更改以下代码行:

    db.collection("demoProviders").document("First Provider").set(myMap)
    

    db.collection("demoProviders").document().set(myMap)
    

    当使用 DocumentReference 的set() 方法时,它:

    覆盖此 DocumentReference 引用的文档。

    所以每次你需要使用另一个参考。正如您在上面的示例中所见,我没有将任何参数传递给 document() 方法:

    返回一个 DocumentReference 指向一个新文档,该文档具有此集合中自动生成的 ID。

    【讨论】:

    • 我还添加了我的 Firestore 结构的图像以供参考。
    • 没有必要。问题是你的参考。请尝试我上面的解决方案并告诉我它是否有效。
    • 我收到下面的错误class R is public, should be declared in a file named R.java
    • 什么时候做什么?当您只是从 document() 方法中删除 "First Provider" 参数时?
    • 是的,一旦我删除了所有这些错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2020-06-28
    • 2019-06-04
    相关资源
    最近更新 更多