gpt4 book ai didi

java - 在android studio中使用firestore将新集合添加到同一文档

转载 作者:行者123 更新时间:2023-12-02 10:33:13 26 4
gpt4 key购买 nike

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

更新:添加了 firestore 结构的图像 enter image description here

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;
}
}

最佳答案

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

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

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

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

Overwrites the document referred to by this DocumentReference.

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

Returns a DocumentReference pointing to a new document with an auto-generated ID within this collection.

关于java - 在android studio中使用firestore将新集合添加到同一文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53483286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com