如何使用查询从已存在的Firebase实时数据库中检索数据

时间:2020-05-01 10:14:51

标签: android firebase firebase-realtime-database

这是我的Firebase数据库

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"

我已经成功连接到数据库 下面是Firebase添加数据代码

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]
    }
}




您可以在数据库中看到我的来源,目的地,票价和时间 到目前为止,我遇到的侦听器只能在添加或修改数据时侦听数据 如何将SOURCE和DESTINATION传递给firebase查询并显示从数据库中提取的所有过滤数据? 预先感谢

0 个答案:

没有答案