0

Ok from the Start, I have a RecyclerView where I want to change the color of the entries, if they are clicked.

The Recyclerview gets a list from a database, where I added a bool value, if the View is selected.

In my holder I check if the view is selected and change the color with a selector.

So far so good, but now there is the problem.

The Activity Starts, the old selected Entry is colored, but when I click on an other Entry the color of the old entry disappears and on the one tapped nothing happens. I have to reclick on the old entry again and from this point the color changing works fine.

method called in viewHolder

 itemView.view_foreground_nav.isSelected = (table.isSelected)

selector

 <selector 
        xmlns:android="http://schemas.android.com/apk/res/android">
        <item 
       android:drawable="@android:color/darker_gray"
       android:state_pressed="true"/>
        <item 
        android:drawable="@android:color/darker_gray"
        android:state_selected="true"/>
        <item 
         android:drawable="@android:color/darker_gray"
         android:state_focused="true"/>
        <item android:drawable="@android:color/white"/>
      </selector>

RelativeLayout from viewholder where color should changed

 android:id="@+id/view_foreground_nav"
    android:layout_width="match_parent"
    android:layout_height="@dimen/ic_delete"
    android:background="@drawable/recycler_nav_item_background">

variable in the Activity which is set in onCreate

private var selectedTable: Table? = null

onClick Method

 if (selectedTable != null) {
                    var oldSelectedTable = selectedTable
                    oldSelectedTable?.isSelected = false
                    productViewModel.updateTable(oldSelectedTable!!)
                    data.isSelected = true
                    productViewModel.updateTable(data)
                    selectedTable = data
                } else {
                    data.isSelected = true
                    productViewModel.updateTable(data)
                    selectedTable = data
                }
                button_add_note.isEnabled = true
                observerProduct(selectedTable)

Diffutil

override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int):Boolean{

return oldListTable[oldItemPosition].idTable == newListTable[newItemPosition].idTable
}

override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return oldListTable[oldItemPosition].title == newListTable[newItemPosition].title&&
                    oldListTable[oldItemPosition].isSelected == newListTable[newItemPosition].isSelected
        }

The ProductViewModel is a lateinit in the MainActivity

 private lateinit var productViewModel: ProductViewModel

In OnCreate I set the ViewModel

 productViewModel = 
 ViewModelProviders.of(this).get(ProductViewModel::class.java)

GenericAdapter Call

      private fun observerTable() {
    productViewModel.getAllTables().observe(this, Observer<List<Table>> { it ->
        adapterNav.setItems(it as MutableList<Any>)
    })

}

Table Dao

     @Query("Select * from main_table")
fun getAllTables(): LiveData<MutableList<Table>>

ProductRepro

private var tableDao: TableDao
private var allTables: LiveData<MutableList<Table>>
init {
    var database: ProductDatabase? = ListMasterApplication.database
    this.tableDao = database!!.tableDao()
    this.allTables = tableDao.getAllTables()

}
var database: ProductDatabase? = ListMasterApplication.database

fun getAllTable(): LiveData<MutableList<Table>> {
    return allTables
}

ProductVieModel

private var productRepository: ProductRepository = ProductRepository()
private var liveDataTable: LiveData<MutableList<Table>>

init {
    this.liveDataTable = productRepository.getAllTable()

}

fun getAllTables():LiveData<MutableList<Table>>{
   return  liveDataTable
}

Observer in MainActivity

  private fun observerTable() {
    productViewModel.getAllTables().observe(this, Observer<MutableList<Table>> { it ->
        adapterNav.setItems(it as MutableList<Any>)
    })

}

In GenericAdapter set Items

fun setItems(items: MutableList<T>) {
    val diffCallback = DiffCallback(listItems ,items)
    val diffResult = DiffUtil.calculateDiff(diffCallback)
    listItems.clear()
    listItems.addAll(items)
    diffResult.dispatchUpdatesTo(this)
}

I logged my Diffutil and on each click on different items the Log was always correct. I logged the selected table variable an it is always correct.

I don't know where to go from this point. It only happens in the start of the activity and then it works fine. Do you have some hints or something, which i could try. If you need some more insight of the code, let me know and I publish more.

Simon
  • 1
  • 1

0 Answers0