数据未添加到Firebase,数据未通过Intent和!TextUtils.isEmpty不起作用



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

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

2) 我使用Intent将数据从一个活动传递到另一个活动,但它没有传递。我在下一个"活动"中使用了textView来设置Text,但它变为null。数据也没有在日志中显示,因为我对Log.d了解很多

3) 我不知道是什么原因!TextUtils.isEmpty不适用于我的CreateEvent2。我需要注释整个if-condition才能转到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>

问题是您正在从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);
}
}
});

最新更新