[英]Radio Button text pushed to Firebase
我正在构建一个应用程序,我希望用户能够将各种食品或饮料产品上传到firebase
。 要求用户输入的信息是图像、产品名称、描述、价格和类别(食品或饮料)。 我使用ImageButton
作为图像,使用EditText
作为名称、描述和价格。 对于我使用RadioGroup
/ RadioButton
的类别。 图片、描述、价格和名称正在推送到firebase
;
但是,我在RadioButtons
上苦苦挣扎。 我想将 RadioButton 文本 Food or Beverage 推送到 Firebase。 我找到了一个关于它的部分教程,但我的西班牙语不太流利,也无法跟上正在发生的一切。
package com.example.nccummings.concertconnectowner;
import android.content.Intent;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
public class AddNewProductContinued extends AppCompatActivity {
private ImageButton productImage;
private static final int GALLREQ = 2;
private EditText name,description,price;
private String stringFoodOrBev;
private RadioGroup bevOrFood;
private RadioButton bevOrFoodOption;
private Uri uri = null;
private StorageReference storageReference = null;
private DatabaseReference mRef;
private FirebaseDatabase firebaseDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_product_cont);
storageReference =
FirebaseStorage.getInstance().getReference("Product Pictures");
mRef = FirebaseDatabase.getInstance().getReference("Product
Information");
name = findViewById(R.id.productName);
description = findViewById(R.id.productDescription);
price = findViewById(R.id.productPrice);
bevOrFood = findViewById(R.id.rgbevorfood);
// This method allows user to select which category the product belongs
//to (food or bev).
bevOrFood.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
bevOrFoodOption = bevOrFood.findViewById(checkedId);
switch (checkedId)
{
case R.id.rbfood:
stringFoodOrBev = bevOrFoodOption.getText().toString().trim();
break;
case R.id.rbbev:
stringFoodOrBev = bevOrFoodOption.getText().toString().trim();
break;
default:
}
}
});
}
// This method allows user to select image from phone
// once image button is clicked.
public void imageProductButtonClicked(View view){
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent,GALLREQ);
}
// Once image has been selected from the phone it gets set set to a uri
//aka url.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLREQ && resultCode == RESULT_OK){
uri = data.getData();
productImage = findViewById(R.id.productImageButton);
productImage.setImageURI(uri);
}
}
// This method adds the information input into the form to Firebase.
public void addProductToMenuButtonClicked(View view){
final String name_text = name.getText().toString().trim();
final String description_text =
description.getText().toString().trim();
final String price_text = price.getText().toString().trim();
if (!TextUtils.isEmpty(name_text) &&
!TextUtils.isEmpty(description_text) && !TextUtils.isEmpty(price_text)){
StorageReference filepath =
storageReference.child(uri.getLastPathSegment());
filepath.putFile(uri).addOnSuccessListener(new
OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
{
final Uri downloadurl = taskSnapshot.getDownloadUrl();
Toast.makeText(com.example.nccummings.concertconnectowner.
AddNewProductContinued.this,"Product Information has been successfully
uploaded.", Toast.LENGTH_SHORT).show();
final DatabaseReference newPost = mRef.push();
newPost.child("Product").setValue(name_text);
newPost.child("Description").setValue(description_text);
newPost.child("Price").setValue(price_text);
newPost.child("pImage").setValue(downloadurl.toString());
newPost.child("Category").setValue(stringFoodOrBev);
}
});
}
}
}
下面是 xml 文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".AddNewProductContinued">
<ImageButton
android:id="@+id/productImageButton"
android:layout_width="match_parent"
android:layout_height="230dp"
android:onClick="imageProductButtonClicked"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="54dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/productName"
android:hint="Enter name of product."
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/productDescription"
android:hint="Enter product description."
/>
<EditText
android:id="@+id/productPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter product price." />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="38dp"
android:id="@+id/rgbevorfood"
>
<RadioButton
android:id="@+id/rbfood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Food" />
<RadioButton
android:id="@+id/rbbev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Beverage" />
</RadioGroup>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="addProductToMenuButtonClicked"
android:text="Add product to menu" />
</LinearLayout>
可以试试这个,我的小项目之一。 它对我有用。
xml
<RadioGroup
android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_male"
android:checked="true" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_female" />
</RadioGroup>
活动
// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MainActivity.this,
radioButton.getText(), Toast.LENGTH_SHORT).show();
//Check whether get the text or not from using Toast
这是我从单选按钮添加数据(性别)并将其发送到firebase并得到结果的方式。
xml
<RadioGroup
android:layout_width="0dp"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints"
app:layout_constraintTop_toBottomOf="@id/et_score"
app:layout_constraintStart_toStartOf="@id/et_score"
app:layout_constraintEnd_toEndOf="@id/et_score"
android:id="@+id/radiogroup"
android:layout_marginTop="5dp"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female"
android:id="@+id/rb_female"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb_male"
android:text="@string/male"
/>
活动
class AddMembersActivity : AppCompatActivity() {
private lateinit var binding : ActivityAddMembersBinding
private lateinit var dbRef : DatabaseReference
private var gender = "female"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityAddMembersBinding.inflate(layoutInflater)
setContentView(binding.root)
dbRef = FirebaseDatabase.getInstance().getReference("Users")
binding.btnSave.setOnClickListener {
saveData()
startActivity(Intent(this , MainActivity::class.java))
finish()
}
binding.rbMale.setOnClickListener {
gender = "male"
binding.rbFemale.isChecked = false
}
binding.rbFemale.setOnClickListener {
gender = "female"
binding.rbMale.isChecked = false
}
}
private fun saveData() {
Log.d("testtest","gender is $gender")
val studentName = binding.etName.text.toString()
val studentClass = binding.etClass.text.toString()
val studentPhone = binding.etPhone.text.toString()
val studentScore :Int = binding.etScore.text.toString().toInt()
val temp = gender
val user = Users(studentName , studentClass , studentPhone , studentScore , temp )
dbRef.child(studentPhone.toString()).setValue(user).addOnCompleteListener {
Toast.makeText(this , "Add Successfully" ,Toast.LENGTH_LONG).show()
}.addOnFailureListener {
Toast.makeText(this , "Add Failed" ,Toast.LENGTH_LONG).show()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.