2

enter image description here Kotlin How and where to declare value so I can get result.By this code I am unable to get search result.As it is showing listview but while searching by path string. Result not showing.

some error comes with searchbar.

I have shared a picture in this if I want to search by note and task or just task then where we have to declare that child. I have declared orderByChild but then also nothing is coming when I search something in searchBar. So help me out in this .

class ToDoOverActivity : AppCompatActivity() {

      lateinit var toolbar: Toolbar
      lateinit var todoOver: ListView
      lateinit var todoOverList: MutableList<TodoClass>
      lateinit var auth: FirebaseAuth
      lateinit var ref: DatabaseReference
      lateinit var user: FirebaseUser
        override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_to_do_over)
         auth = FirebaseAuth.getInstance()
         user = auth.currentUser!!
         todoOver = findViewById(R.id.todoOver)
         todoOverList = mutableListOf()
         toolbar = findViewById(R.id.toolbar)
         setUpToolbar()
         val searchBar = findViewById(R.id.searchBar) as MaterialSearchBar
            searchBar.setHint("Search...")
            searchBar.isSearchEnabled
         ref = FirebaseDatabase.getInstance().getReference().child(user.uid).child("To Do Over")
            ref.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(snapshot: DataSnapshot) {
              if (snapshot!!.exists()){
              todoOverList.clear()
                for (h in snapshot.children){
                val overTask = h.getValue(TodoClass::class.java)
                todoOverList.add(overTask!!)
         val adapter = TodoOverAdapter(this@ToDoOverActivity, R.layout.todo_over_layout, todoOverList)
                todoOver.adapter = adapter
         searchBar.addTextChangeListener(object: TextWatcher{
               override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
               override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                 val searchText = searchBar.text.toString()
                 searchTask(searchText.toLowerCase())
                 adapter.getFilter().filter(p0)
     }
         override fun afterTextChanged(p0: Editable?) {
             }
           })
          }
         }
         override fun onCancelled(error: DatabaseError) {
             }
            })
         }
          private fun searchTask(searchText: String){
            ref.child("To Do Over").orderByChild("task").startAt(searchText)
                         .endAt("$searchText\uf8ff")
        }
        fun setUpToolbar(){
        setSupportActionBar(toolbar)
        supportActionBar?.setHomeButtonEnabled(true)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        }
    }
  • Data is read from Firebase asynchronously, so any code that needs the results need to be inside the `onDataChange` or called from tere. See https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 for an example and more info. – Frank van Puffelen Nov 18 '20 at 18:11
  • I have edit the question . Have a look and please advice – Shriya Gupta Nov 19 '20 at 18:10
  • All I see you doing with the search text is `ref.child("To Do Over").orderByChild("task").startAt(searchText).endAt("$searchText\uf8ff")`. This creates a query, but nothing will be read until you attach a listener to that query (just as you already do with `addValueEventListener`). – Frank van Puffelen Nov 19 '20 at 18:24
  • Can you please tell me what I have to do and which listener to call and what to declare inside that. – Shriya Gupta Nov 19 '20 at 18:30

1 Answers1

0

Try separating the "To Do Over" in your firebase with hyphens or CamelCase. That is to "To-Do-Over" or ToDoOver". The aim is to make it one word and eliminate the spaces