数据未添加到Firebase,数据未通过Intent传递且!TextUtils.isEmpty无法正常工作

时间:2017-01-31 14:46:03

标签: android firebase firebase-realtime-database

所以我正在使用Firebase作为我的数据库创建一个事件应用程序。我是业余开发者,所以请耐心等待。我遇到了三个主要问题:

1)我的主要问题是数据没有写入Firebase。我已在数据库模块中将规则更改为“true”。我还有正在上传的用户输入的图像,但是没有上传event_title等数据

2)我使用Intent将数据从一个活动传递到另一个活动,但它没有通过。我在下一个Activity的setText上使用了textView,但它变为null。我对Log.d

的了解并不是日志中的数据

3)我不是出于什么原因!TextUtils.isEmpty不能用于我的CreateEvent2。我需要评论整个if - 条件才能转到CreateEvent3。但是它在CreateEvent3.java中工作。

我尝试了很多方法,但我不知道这些错误会产生什么原因。

这是我的代码:

CreateEvent2.java

package com.example.admin.college20;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CreateEvent2 extends AppCompatActivity {

    private EditText mEventTitle, mEventLocation, mEventCategory, mContact;
    private Button mNextButton;

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

        mEventTitle = (EditText) findViewById(R.id.event_title);
        mEventLocation = (EditText) findViewById(R.id.event_location);
        mEventCategory = (EditText) findViewById(R.id.event_category);
        mContact = (EditText) findViewById(R.id.contact_info);
        mNextButton = (Button) findViewById(R.id.nextButton);

        final String event_title = mEventTitle.getText().toString().trim();
        final String event_location = mEventLocation.getText().toString().trim();
        final String event_category = mEventCategory.getText().toString().trim();
        final String contact = mContact.getText().toString().trim();

        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**if (!TextUtils.isEmpty(event_title)
                        && !TextUtils.isEmpty(event_location)
                        && !TextUtils.isEmpty(event_category)
                        && !TextUtils.isEmpty(contact)) {**/

                    Intent i = new Intent(CreateEvent2.this, CreateEvent3.class);
                    i.putExtra("title", event_title);
                    i.putExtra("location", event_location);
                    i.putExtra("category", event_category);
                    i.putExtra("contact", contact);
                    startActivity(i);
                }


        });
    }
}

activity_create_event2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.admin.college20.CreateEvent2"
    android:background="@drawable/create_event_1">

    <EditText
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:hint="Event Title"
        android:layout_marginTop="90dp"
        android:id="@+id/event_title"
        android:inputType="textMultiLine"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/event_location"
        android:layout_alignEnd="@+id/event_location" />

    <EditText
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:hint="Event Location"
        android:id="@+id/event_location"
        android:inputType="textMultiLine"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/contact_info"
        android:layout_alignStart="@+id/contact_info" />

    <EditText
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:hint="Event Category"
        android:id="@+id/event_category"
        android:inputType="textMultiLine"
        android:layout_marginTop="32dp"
        android:layout_below="@+id/event_location"
        android:layout_alignLeft="@+id/contact_info"
        android:layout_alignStart="@+id/contact_info" />

    <EditText
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:hint="Contact Number"
        android:id="@+id/contact_info"
        android:inputType="textMultiLine"
        android:layout_above="@+id/nextButton"
        android:layout_marginBottom="20dp"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:text="New Button"
        android:id="@+id/nextButton"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="27dp" />

</RelativeLayout>

CreateEvent3.java

package com.example.admin.college20;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnFailureListener;
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 CreateEvent3 extends AppCompatActivity {
    private EditText mEventDesc, mEventFBUrl, mEventWebLink;
    private TextView hello;
    private Button upload_image_button, done_button;


    private ProgressDialog progressDialog;
    private static final int GALLERY_INTENT = 1;
    private Uri imageUri = null;

    private DatabaseReference mDatabaseReference;
    private StorageReference mStorageRef;


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

        mEventDesc = (EditText) findViewById(R.id.event_desc);
        mEventFBUrl = (EditText) findViewById(R.id.fb_event_url);
        mEventWebLink = (EditText) findViewById(R.id.event_weblink);
        upload_image_button = (Button) findViewById(R.id.upload_image_button);
        done_button = (Button) findViewById(R.id.done_button);
        hello = (TextView) findViewById(R.id.hello);

        mDatabaseReference = FirebaseDatabase.
                getInstance().
                getReference().
                child("Event");
        mStorageRef = FirebaseStorage.getInstance().getReference();

        progressDialog = new ProgressDialog(this);

        upload_image_button.setVisibility(View.VISIBLE);
        upload_image_button.setBackgroundColor(Color.TRANSPARENT);

        upload_image_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, GALLERY_INTENT);
            }
        });
        done_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startPosting();
                Toast.makeText(CreateEvent3.this, "Button Selected", Toast.LENGTH_SHORT).show();
            }
        });

    }

    public void startPosting() {

        progressDialog.setMessage("Uploading the Data");

        Intent in = getIntent();
        Bundle bundle = in.getExtras();
        final String event_title = getIntent().getStringExtra("title");
        final String event_location = getIntent().getStringExtra("location");
        final String event_category = getIntent().getStringExtra("category");
        final String contact = getIntent().getStringExtra("contact");

        final String event_desc = mEventDesc.getText().toString().trim();
        final String event_weblink = mEventWebLink.getText().toString().trim();
        final String event_fb_url = mEventFBUrl.getText().toString().trim();

        Log.d("Event Tile", event_title);
        Log.d("Event Location", event_location);
        Log.d("Event Category", event_category);
        hello.setText(event_title);

        if (!TextUtils.isEmpty(event_desc)
                && !TextUtils.isEmpty(event_weblink)
                && !TextUtils.isEmpty(event_fb_url)
                && imageUri != null) {
            progressDialog.show();

            StorageReference filepath = mStorageRef.child("Images").child(imageUri.getLastPathSegment());

            filepath.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Uri uri = taskSnapshot.getDownloadUrl();

                    DatabaseReference newEvent = mDatabaseReference.push();
                    newEvent.child("title").setValue(event_title);
                    newEvent.child("location").setValue(event_location);
                    newEvent.child("category").setValue(event_category);
                    newEvent.child("contact").setValue(contact);
                    newEvent.child("desc").setValue(event_desc);
                    newEvent.child("web").setValue(event_weblink);
                    newEvent.child("fb").setValue(event_fb_url);
                    newEvent.child("imageUrl").setValue(uri.toString());

                    progressDialog.dismiss();
                    startActivity(new Intent(CreateEvent3.this, MainPage1.class));
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(CreateEvent3.this, "Upload Failed :(", Toast.LENGTH_SHORT).show();
                }
            });
        }
        else{
            Toast.makeText(this, "Fill in the details", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GALLERY_INTENT && resultCode == RESULT_OK) {
            imageUri = data.getData();
        }
    }

}

activity_create_event3.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.example.admin.college20.CreateEvent3"
    android:background="@drawable/create_event_2">


    <EditText
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:id="@+id/event_desc"
        android:layout_marginTop="144dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:inputType="textMultiLine"
        android:hint="Enter short description here..."
        />

    <EditText
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:id="@+id/fb_event_url"
        android:layout_below="@+id/event_desc"
        android:layout_alignLeft="@+id/event_desc"
        android:layout_alignStart="@+id/event_desc"
        android:layout_marginTop="134dp" />

    <EditText
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:id="@+id/event_weblink"
        android:layout_marginTop="49dp"
        android:layout_below="@+id/fb_event_url"
        android:layout_alignLeft="@+id/fb_event_url"
        android:layout_alignStart="@+id/fb_event_url" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="300dp"
        android:layout_height="60dp"
        android:id="@+id/upload_image_button"
        android:layout_marginTop="50dp"
        android:layout_below="@+id/event_desc"
        android:layout_alignLeft="@+id/event_desc"
        android:layout_alignStart="@+id/event_desc" />

    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:id="@+id/done_button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="15dp" />

    <TextView
        android:text="Hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/event_desc"
        android:layout_alignEnd="@+id/event_desc"
        android:layout_marginRight="49dp"
        android:layout_marginEnd="49dp"
        android:layout_marginTop="33dp"
        android:id="@+id/hello" />


</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

问题是你从CreateEvent2的onCreate方法的EditText中获取文本。你应该在你的OnClickListener上得到它。像这样:

mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            final String event_title = mEventTitle.getText().toString().trim();
            final String event_location = mEventLocation.getText().toString().trim();
            final String event_category = mEventCategory.getText().toString().trim();
            final String contact = mContact.getText().toString().trim();
                if (!TextUtils.isEmpty(event_title)
                        && !TextUtils.isEmpty(event_location)
                        && !TextUtils.isEmpty(event_category)
                        && !TextUtils.isEmpty(contact)) {

                    Intent i = new Intent(CreateEvent2.this, CreateEvent3.class);
                    i.putExtra("title", event_title);
                    i.putExtra("location", event_location);
                    i.putExtra("category", event_category);
                    i.putExtra("contact", contact);
                    startActivity(i);
                }
               }

        });