0

Here is my Firebase database

bus-dhundo
-M5ljkUYZVNG9uTNxSVZ
dst: "Kolkata"
fr:   "222"
src:  "Siliguri"
tm:   "8.30 am"

-M5lmU3zfSWsZBJv1_Js
dst: "Kolkata"
fr: "530"
src: "Siliguri"
tm:  "8.50 pm"

I have already successfully connected to my database below is firebase adding data code

package com.example.busdhundo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

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 MainActivity extends AppCompatActivity {
    private static final String TAG = "sid";

    ////EDIT TEXT///////////////////

    EditText source;
    EditText destination;
    EditText time;
    EditText fare;
    Button save;
    Button read;

    //////////////////////////////

    ///////Firebase ref//////////
    public DatabaseReference databaseReference;
    ////////////////////////////

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

        ////////Find view by id section//////////

        source = (EditText)findViewById(R.id.SOURCE);
        destination = (EditText)findViewById(R.id.DESTINATION);
        time = (EditText)findViewById(R.id.TIME);
        fare = (EditText)findViewById(R.id.FARE);
        save = (Button) findViewById(R.id.SWITCH);

        /////////////////////////////////////////
        read = (Button)findViewById(R.id.read_fire);
        ////////////////////////////////////////
        databaseReference = FirebaseDatabase.getInstance().getReference();

        ////////////On button click//////////////

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getConnectionState();
                int z = saveData();
                Log.d(TAG, "onClick: "+z);


            }
        });
        read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,ReadData.class );
                startActivity(intent);
            }
        });
    }

    public int saveData()
    {
        String src2 = source.getText().toString().trim();
        String dst2 = destination.getText().toString().trim();
        String tm2 = time.getText().toString().trim();
        String fr2 = fare.getText().toString().trim();
        //System.out.println(src2);
        Log.d(TAG, "saveData: " +src2);
        String x = databaseReference.toString();
        SaveData saveData = new SaveData(src2,dst2,tm2,fr2);

        //databaseReference.child("ID").setValue(saveData);
        databaseReference.push().setValue(saveData);
        return (1);
    }
    private void getConnectionState() {
        // [START rtdb_listen_connected]
        DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
        connectedRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                boolean connected = snapshot.getValue(Boolean.class);
                if (connected) {
                    Log.d(TAG, "connected");
                } else {
                    Log.d(TAG, "not connected");
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Log.w(TAG, "Listener was cancelled");
            }
        });
        // [END rtdb_listen_connected]
    }
}




As you can see in the database I have source, destination, fare and time so far I came across the listeners that can listen to only data when data is either added or modified How to pass the SOURCE and DESTINATION to firebase query and display all the filtered data fetch from the database? thanks in advance

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • What is the exact query you want to perform? – Alex Mamo May 01 '20 at 10:44
  • Form two Editetxt I want to retrieve "SOURCE -location" and "DESTINATION-location" input and show all the result matching with these two locations. – Kabiraa Boyz May 01 '20 at 10:56
  • Please check the duplicate to see how you can filter the results based on two properties. – Alex Mamo May 01 '20 at 11:12
  • Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. In your case that could be a property: `"src_dst": "Siliguri_ Kolkata"`. For a longer example of this and other approaches, see my answer here: https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen May 01 '20 at 14:12

0 Answers0