2017-01-31 1 views
-1

Ich erstelle also eine Ereignis-App, die Firebase als meine Datenbank verwendet. Ich bin ein Amateur-Entwickler also bitte ertragen mit mir. Es gibt 3 Hauptprobleme, mit denen ich konfrontiert wurde:Daten werden nicht zu Firebase hinzugefügt, Daten werden nicht durch Intent weitergeleitet und! TextUtils.isEmpty funktioniert nicht

1) Mein Hauptproblem ist, dass Daten nicht zu Firebase geschrieben werden. Ich habe die Regeln im Datenbankmodul auf 'wahr' gesetzt. Ich habe auch eine Bildeingabe durch den Benutzer, der hochgeladen wird, aber die Daten wie event_title und andere werden nicht hochgeladen

2) Ich habe Intent verwendet, um Daten von einer Aktivität an eine andere zu übergeben, aber es wird nicht übergeben. Ich habe eine Textansicht für die nächste Aktivität verwendet, um Text festzulegen, aber sie ändert sich in null. Die Daten werden auch nicht im Log angezeigt, so viel weiß ich über Log.d

3) Ich nicht aus welchem ​​Grund! TextUtils.isEmpty funktioniert nicht auf meinem CreateEvent2. Ich muss die gesamte if - Bedingung kommentieren, um zum CreateEvent3 zu gelangen. Es funktioniert jedoch in CreateEvent3.java.

Ich habe viele Methoden ausprobiert, aber ich weiß nicht, aus welchen Gründen diese Fehler kommen.

Hier ist mein Code:

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> 
+0

Wenn Sie die gesamte if - Bedingung kommentieren, werden die Daten in der Datenbank gespeichert? –

+0

Nein, es wird nicht gespeichert –

Antwort

1

Das Problem ist, dass Sie den Text aus dem EditText auf Ihrer CreateEvent2 onCreate-Methode erhalten. Sie sollten es auf Ihrem OnClickListener erhalten. Wie folgt:

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

     }); 
+0

Dank dies das Problem gelöst, obwohl ich nicht verstehe warum? –

+0

Siehe, die 'onCreate'-Methode wird aufgerufen, wenn die Aktivität gestartet wird. Und du hast "getText" aufgerufen, wenn die Aktivität gestartet wurde (und die editTexts in diesem Moment leer sind). Jetzt wird Ihre Methode 'onClick' aufgerufen, wenn Sie die Taste drücken. Zu diesem Zeitpunkt ist der EditText nicht mehr leer (weil Sie ihn ausgefüllt haben). –

+0

Oh gut, ich habe meinen Fehler. Vielen Dank ! –

Verwandte Themen