2

I have a Room db and have created a model and viewModel. I was wondering how to make an autocompletetextview work with the database data and the viewmodel to filter the customer list as the user types

ViewModel

class CustomerVM : ViewModel() {
    private val customers: MutableLiveData<List<Customer>> by lazy {
        loadCustomers()
    }

    fun getCustomers(): LiveData<List<Customer>> {
        return customers
    }

    private fun loadCustomers() : MutableLiveData<List<Customer>> {
        return DataModel.getInstance().roomDb.CustomerDao().getAllCustomers() as MutableLiveData<List<Customer>>
    }
}
denvercoder9
  • 2,979
  • 3
  • 28
  • 41
user3074140
  • 733
  • 3
  • 13
  • 30
  • Do this. `val adapter = AutocompleteArrayAdapter(context!!, R.layout.location_drop_down_autocomplete, yourListOfCustomers) yourAutoComplete.setAdapter(adapter)` – OhhhThatVarun Dec 02 '19 at 10:09

2 Answers2

0

If i understand you want to have as entries of your autocomplete text view the data that you have in the internal DB.

To do that i create my custom ViewModel with allStudentsData, repository an listen from activity

  1. Repository Class A repository that is listening directly from DB

    val allStudents: LiveData<List<Student>> = studentDao.getAll()

  2. ViewModel Class

   init {
        val studentsDao = AppDatabase.getDatabase(application,viewModelScope).studentsDao()
        studentRepository = StudentRepository(studentsDao)
        allStudents = studentRepository.allStudents
    }
  1. Activity Class
    private lateinit var studentViewModel: StudentViewModel

    public override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        studentViewModel = ViewModelProvider(this).get(StudentViewModel::class.java)
        studentViewModel.allStudents.observe(this, Observer { students ->
            // Update the cached copy of the students in the adapter.
            students?.let {
                val arr = mutableListOf<String>()

                for (value in it) {
                    arr.add(value.name)
                }
                val adapter: ArrayAdapter<String> =
                    ArrayAdapter(this, android.R.layout.select_dialog_item, arr)
                names.setAdapter(adapter)
            }
        })
    }

In the activity we have a viewModel variable that observe the data that is change when new record is insert in DB. When we have new data Observer {} is called, so with the new list of students we create a mutable list, we add all students and we set the adapter

By doing this, when data change in DB

DB ====> Repository ====> ViewModel ====> Activity

RonU
  • 5,525
  • 3
  • 16
  • 13
Dak28
  • 259
  • 1
  • 8
-1
 step 1:create custom autocomplete text view

     example:https://spapas.github.io/2019/04/05/android-custom-filter-adapter/

 step2:get data from room dp

       @Query("SELECT * FROM Gender WHERE name == :name")
       fun getGenderByName(name: String): List<Gender>

 step3:update this data to  custom autocomplete adapter
Nagendran P
  • 96
  • 1
  • 5