在发布到Firestore时对radioButton使用相同的值 [英] using same value for radioButton when posting to firestore

查看:44
本文介绍了在发布到Firestore时对radioButton使用相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的布局中有2个 radioButtons .当询问是否有保险时,一种用于,另一种用于 no .当我单击我的 save 按钮并将数据发布到 firestore 时,它同时显示 NO INSURED YES INSURED .我设置代码的方式就是为什么这样设置,但是我想做的是提供一个来源,如果该人单击 yes no 然后在中单击firestore 我将有 Insured:是 Insured:否.我将代码发布到

I have a 2 radioButtons in my layout. One is for yes and the other is for no when asked if insured or not. When I click my save button and the data posts to firestore, it shows both NO INSURED and YES INSURED. The way I have my code set up is why it's that way but what I want to do is have one source where if the person clicks yes or no then in firestore I will have Insured : Yes or Insured : No. I will post my code down below

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.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.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 = "Yes Insured";
    private static final String KEY_NO_INSURED = "No 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;


    // 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);
        my_spinner = (Spinner)findViewById(R.id.mySpinner);
        fixedRadioButton = findViewById(R.id.fixedRadioButton);
        hourlyRadioButton = findViewById(R.id.hourlyRadioButton);
        ageEditText = findViewById(R.id.ageEditText);
        dollarEditText = findViewById(R.id.dollarEditText);
        yesButton = findViewById(R.id.yesRadioButton);
        noButton = findViewById(R.id.noRadioButton);



    }

    @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) {

    }



        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();

        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_FIXED, fixed_radioButton);
        myMap.put(KEY_HOURLY, hourly_Radiobutton);
        myMap.put(KEY_AGE_VALE, age);
        myMap.put(KEY_DOLLAR_VALUE, dollar);
        myMap.put(KEY_YES_INSURED, yes_button);
        myMap.put(KEY_NO_INSURED, no_button);
        myMap.put(KEY_SPINNER_VALUE , spinner);

        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();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(ProviderSignUp.this, "Error!", Toast.LENGTH_SHORT).show();
                Log.d(TAG, e.toString());
            }
        });
    }
}

推荐答案

第一件事,

当我单击我的 save 按钮并将数据发布到 firestore

根据您的代码,这没有发生.您应该使用 OnClickListener 方法来实现您的想法.我建议仔细阅读文档.

This is not happening according to your code. You should use OnClickListener method to implement your thought. I suggest going through this documentation.

希望,您正在为 RadioButton 使用 RadioGroup .这将不允许一次选择多个 RadioButton .此处是实现 RadioGroup 的方法

Hope, you are using RadioGroup for your RadioButton. That won't allow more than one RadioButton to be selected at a single time. Here is how you can implement RadioGroup.

使用单个键存储布尔值,而不是在 Firestore 中为 RadioButton 状态存储多个键值对.试试这个:

Use a single key to store a boolean value instead of storing multiple key-value pairs for RadioButton state in Firestore. Try this :

if (radioGroup.getCheckedRadioButtonId() == R.id.yesRadioButton){
    myMap.put("isInsured", true);
} else {
    myMap.put("isInsured", false);
}

代替此:

myMap.put(KEY_YES_INSURED, yes_button);
myMap.put(KEY_NO_INSURED, no_button);

如果正确执行了所有上述步骤,则数据库应为 RadioButton 状态存储一个值.尝试让我们知道是否可行.

If you do all the mentioned steps correctly your database should store one value for your RadioButton states. Try and let us know if this worked or not.

这篇关于在发布到Firestore时对radioButton使用相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆