I tried so many time to implement to search my android Firebase database.
Query: query = dateRef.orderByChild("phone").equalTo(searchPhoneNumber);
I don't know how to set searchPhoneNumber in EditText value. But I know EditText and one button using to search phone numbers. But I need Java code. Eg: if I enter phone numbers in EditText and I press button, after I get a output in same page.
Here I attached my Firebase screenshot and my code.
Firebase database screenshot
java
public class ViewProduction extends AppCompatActivity {
private RecyclerView mRecyclerView;
private Adapater mAdapter;
private ProgressBar mprogress;
private DatabaseReference mDatabaseRef;
private List<Datastore> mUploads;
EditText edit;
Button btnsearch;
@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) {
// called search() method on button click.
search();
}
});
public void search(){
edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference dateRef = rootRef.child("Rajadriving").child("9-6-2018");
Query query = dateRef.orderByChild("phone").equalTo(s.toString());
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);
mAdapter.notifyDataSetChanged();
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">
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/progress"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/progress"
android:hint="enter phone" />
<Button
android:id="@+id/btnsearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/recyclerj"
android:layout_marginLeft="21dp"
android:layout_marginStart="21dp"
android:layout_toEndOf="@+id/edit"
android:layout_toRightOf="@+id/edit"
android:text="Search" />
<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/recyclerj">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
I hope this will work for you.
Use Textwatcher()
to implement search functionality.how to implement given below.
btnsearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// called search() method on button click.
search();
}
});
public void search(){
edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference dateRef = rootRef.child("Rajadriving").child("9-6-2018");
Query query = dateRef.orderByChild("phone").equalTo(s.toString());
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);
mAdapter.notifyDataSetChanged();
mprogress.setVisibility(View.INVISIBLE);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(ViewProduction.this, databaseError.getMessage(),Toast.LENGTH_SHORT).show();
mprogress.setVisibility(View.INVISIBLE);
}
});
}
}
}
To solve this, please change the following line of code:
Query query = dateRef.orderByChild("phone").equalTo("searchPhoneNumber");
to
Query query = dateRef.orderByChild("phone").equalTo(searchPhoneNumber);
See, no quotation marks? You need to search the database after the value that your variable searchPhoneNumber
holds and not after the searchPhoneNumber String which is obvious is not a phone number.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.