Ad

Android Firebase Database Using Material Search Filter All Child Value

i am trying to search data from firebase database. but i don't know. here i attached my firebase database screenshot. In my firebase database, there are name, phone, datefrom and dateto that are present. I want to search data from that if I enter phone number. I want to get result that name,phone, datefrom and dateto. give some example for search material.

Firebase screenshot

java

public class ViewProduction extends AppCompatActivity {

private RecyclerView mRecyclerView;
private Adapater mAdapter;

private ProgressBar mprogress;

private DatabaseReference mDatabaseRef;
private List<Datastore> mUploads;

private TextView date_filter;
Calendar mCurrentDate;
int day, month, year;

EditText edit;
Button btnsearch;

String searchPhoneNumber; //Added

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



    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    edit =(EditText)findViewById(R.id.edit);

    btnsearch = (Button)findViewById(R.id.btnsearch);


    ActionBar actionBar = getSupportActionBar();
    actionBar.setTitle("Production Details");

    mRecyclerView = findViewById(R.id.recyclerj);
    mRecyclerView.setHasFixedSize(true);

    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    mprogress = findViewById(R.id.progress);

    mUploads = new ArrayList<>();

    mDatabaseRef = FirebaseDatabase.getInstance().getReference("Rajadriving");

    btnsearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            searchPhoneNumber = edit.getText().toString().trim();

        }
    });



    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();

    DatabaseReference dateRef = rootRef.child("Rajadriving").child("9-6-2018");
    Query query = dateRef.orderByChild("phone").equalTo("searchPhoneNumber");


    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {



            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Datastore upload = postSnapshot.getValue(Datastore.class);
                mUploads.add(upload);





            }


            mAdapter = new Adapater(ViewProduction.this, mUploads);

            mRecyclerView.setAdapter(mAdapter);
            mprogress.setVisibility(View.INVISIBLE);



        }



        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(ViewProduction.this, databaseError.getMessage(),Toast.LENGTH_SHORT).show();
            mprogress.setVisibility(View.INVISIBLE);
        }
    });
}





@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == android.R.id.home){
        this.finish();
    }
    return super.onOptionsItemSelected(item);
}

}

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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.udayaj.rajadriving.ViewProduction">




<ProgressBar
    android:id="@+id/progress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"/>


<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/table01"
    android:scrollbars="vertical"
    android:id="@+id/recyclerji">

</android.support.v7.widget.RecyclerView>

</RelativeLayout>

give some example for search material.

Ad

Answer

To solve this, you need to use a query like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference dateRef = rootRef.child("Rajadriving").child("9-6-2018");
Query query = dateRef.orderByChild("phone").equalTo(searchedPhoneNumber);

for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
    Datastore upload = postSnapshot.getValue(Datastore.class);
    mUploads.add(upload);
}
Ad
source: stackoverflow.com
Ad