Radio Button文本被推送到Firebase

时间:2018-05-27 03:19:04

标签: java android firebase radio-button

我正在构建一个应用程序,我希望用户能够将各种食品或饮料产品上传到null。要求用户输入的信息是图像,产品名称,描述,价格和类别(食品或饮料)。我使用firebase表示图片,ImageButton表示名称,说明和价格。对于我正在使用EditText / RadioGroup的类别。图片,说明,价格和名称正在推向RadioButton;

然而,我正在与firebase斗争。我想将RadioButton文本Food或Beverage推送到Firebase。我找到了一个部分教程,但我不会说流利的西班牙语,也无法跟踪所发生的一切。

RadioButtons

以下是xml文件。

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

}
});
}

}


}

1 个答案:

答案 0 :(得分:0)

可以尝试这个,我的一个小项目。它适用于我。

<强> 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