rut_0_1
rut_0_1

Reputation: 761

Firebase looping continuously with new data being added unnecessarily to nodes while using Android Studio

I have 2 'create event' activities, where a person can enter the details of an event and upload them to Firebase. The task seems pretty simple but for some reason Firebase Database is looping continuously and adding data unnecessarily. I want an Event node with each event details as child nodes

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

        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 if(event_title !=null && event_location !=null && event_category!=null ){
                Intent i = new Intent(CreateEvent2.this, CreateEvent3.class);
                i.putExtra("title", event_title);
                i.putExtra("location", event_location);
                i.putExtra("category", event_category);
                startActivity(i);
            }
        }

        });

    }
}

CreateEvent3.java

package com.example.admin.college20;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
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 com.firebase.client.Firebase;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class CreateEvent3 extends AppCompatActivity {
    private EditText mEventDesc, mEventFBUrl, mEventWebLink;
    private Button upload_image_button, done_button;
    private static final int GALLERY_INTENT = 1;
    private String event_desc, event_weblink, event_fb_url;
    private ProgressDialog progressDialog;


    DatabaseReference mDatabaseReference;


    @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);
        mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Event");
        progressDialog = new ProgressDialog(this);

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

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

    @Override
    protected void onStart() {
        super.onStart();

        done_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    startPosting();

                    Intent i = new Intent(CreateEvent3.this, MainPage1.class);
                    startActivity(i);
                }

        });
    }

    private void startPosting() {

        progressDialog.setMessage("Uploading");
        progressDialog.show();
        Bundle bundle = getIntent().getExtras();
        final String event_title = bundle.getString("title");
        final String event_location = bundle.getString("location");
        final String event_category = bundle.getString("category");

        mDatabaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                DatabaseReference databaseReference = mDatabaseReference.push();
                databaseReference.child("Event Title").setValue(event_title);
                databaseReference.child("Event Location").setValue(event_location);
                databaseReference.child("Event Category").setValue(event_category);
                databaseReference.child("Event Description").setValue(event_desc);
                databaseReference.child("Event Weblink").setValue(event_weblink);
                databaseReference.child("Event FB").child(event_fb_url);
                progressDialog.dismiss();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

}

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>

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" />

</RelativeLayout>

Upvotes: 0

Views: 1640

Answers (3)

Razvan
Razvan

Reputation: 292

I had the same problem. I wanted to use addValueEventListener because I had to get a value, but the value was constantly changing when I set a new one.

Use addListenerForSingleValueEvent() instead of addValueEventListener().

Upvotes: 1

Lewis McGeary
Lewis McGeary

Reputation: 7932

Yep, this is absolutely a loop.

First to explain what's going on. The ValueEventListener set on your event node is saying: whenever something changes on the Event node, run the onDataChange method. And the onDataChange method is saying: make this change on the Event node. Hence you're into a loop.

The code that's in the ValueEventListener seems to be in the wrong place; onDataChange is for what you want your app to do with new data at that location.

You could move your push() and setValue() stuff out of the listener and put it directly in the startPosting() method and it will only execute once.

Upvotes: 3

Rahul
Rahul

Reputation: 10635

Reason of continuous loop is you are pushing data on node where you have added valueEventListener.

So when you called startPosting() -> onDataChange -> It try to push data on node -> Node is changed -> onDataChange -> It try to push data on node -> Node is changed.

This loop will be continuous. I don't found any use of adding addValueEventListener while posting data.

Upvotes: 1

Related Questions