I was trying to get some data from my Firebase realtime-database as followed generally, but I couldn’t figure out where in the code the problem was.
The code is attached:
package com.example.abed.smit; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.firebase.ui.database.FirebaseRecyclerAdapter; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.Query; public class FindMedicineActivity extends AppCompatActivity { private Button SearchButton_medicine; private EditText SearchInput_medicine; private RecyclerView SearchResult_medicine; private DatabaseReference allDocdatabaseref; FirebaseRecyclerAdapter<FindMedicine,FindMedicineViewHolder> firebaseRecyclerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_find_medicine); SearchResult_medicine = (RecyclerView) findViewById(R.id.search_result_list2); SearchResult_medicine.setHasFixedSize(true); SearchResult_medicine.setLayoutManager(new LinearLayoutManager(this)); SearchButton_medicine = findViewById(R.id.search_people_btn2); SearchInput_medicine = (EditText) findViewById(R.id.Search_box_input2); allDocdatabaseref = FirebaseDatabase.getInstance().getReference().child("MEDICINE"); SearchButton_medicine.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String searchBoxInput1 = SearchInput_medicine.getText().toString().trim(); SearchMedicine(searchBoxInput1); } }); } private void SearchMedicine(String searchBoxInput1) { Toast.makeText(this, "searching..", Toast.LENGTH_LONG).show(); Query searchmedicinequery = allDocdatabaseref.orderByChild("medicinename").startAt(searchBoxInput1).endAt(searchBoxInput1 + "uf8ff"); firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<FindMedicine, FindMedicineViewHolder>( FindMedicine.class, R.layout.all_medicine_display_layout, FindMedicineViewHolder.class, searchmedicinequery ) { @Override protected void populateViewHolder(FindMedicineViewHolder viewHolder, FindMedicine model, int position) { viewHolder.setMedicinename(model.getMedicinename()); } }; SearchResult_medicine.setAdapter(firebaseRecyclerAdapter); firebaseRecyclerAdapter.startListening(); } private class FindMedicineViewHolder extends RecyclerView.ViewHolder { View mView; public FindMedicineViewHolder(View itemView) { super(itemView); mView = itemView; } public void setMedicinename(String medicinename) { TextView Docname = mView.findViewById(R.id.all_user_profile_name2); Docname.setText(medicinename); } } }
I also use getter:
package com.example.abed.smit; public class FindMedicine { public String medicinename; public FindMedicine(String medicinename) { this.medicinename = medicinename; } public String getMedicinename() { return medicinename; } }
xml all allmedicine
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="#000000"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_margin="5dp"> <TextView android:id="@+id/all_user_profile_name2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="UserFullNAme" android:textColor="#91DC5A" android:textSize="18sp" android:textStyle="bold"/> </LinearLayout> </LinearLayout>
find_medicine xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" tools:context=".FindMedicineActivity"> <RelativeLayout android:id="@+id/myLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"> <EditText android:id="@+id/Search_box_input2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:layout_marginLeft="40dp" android:layout_marginRight="40dp" android:drawableLeft="@drawable/ic_action_name" android:hint="Enter Bmdc Number" android:textColor="#FFFFFFFF" android:textColorHint="#FFFFFFFF" android:textSize="16sp" android:textStyle="bold" /> <Button android:id="@+id/search_people_btn2" android:layout_width="170dp" android:layout_height="30dp" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginStart="94dp" android:layout_marginTop="67dp" android:background="@drawable/buttonshape" android:shadowColor="#A8A8A8" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="5" android:text="Confirm" android:textColor="#FFFFFF" android:textSize="20sp" android:layout_alignParentLeft="true" android:layout_marginLeft="94dp" /> </RelativeLayout> <android.support.v7.widget.RecyclerView android:id="@+id/search_result_list2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/myLayout2" android:layout_margin="10dp" android:background="#000000" > </android.support.v7.widget.RecyclerView> </RelativeLayout>
Advertisement
Answer
Change your query to –
Query searchmedicinequery = allDocdatabaseref.orderByChild("name").startAt(searchBoxInput1).endAt(searchBoxInput1 + "uf8ff");
And according to firebase doc – The class has an empty constructor, which is required for Firebase’s automatic data mapping.
So change your model as –
public class FindMedicine { public String name; public FindMedicine() { } public FindMedicine(String name) { this.name = name; } public String getName() { return name; } }