在 kotlin 中更改列表时适配器不会更改

问题描述 投票:0回答:1

我有一个列表和一个适配器,我从服务中获取列表,当列表到达时,我用

submitlist
将其交给适配器。当我创建
submitlist
时,列表会正确显示,但当它转到适配器时,它会显示为空。我找不到原因,你能帮我吗?这是我的代码

ProfileStatisticAdapter

class ProfileStatisticAdapter(
    private val salesRange: List<String>?,
    private val itemClickListener: ((Int) -> Unit)? = null
) : ListAdapter<String, ProfileStatisticAdapter.StatisticViewHolder>(StatisticDiffCallback) {

    private val salesOptions = listOf(
        "last 1 day sale",
        "lasy 7 day sale" ,
        "last 30 day sale"
    )

    override fun getItemCount(): Int {
        Log.d("ProfileStatistics salesRangeLiveData","salesRange value: "+salesRange)
        return salesRange?.size ?: 0
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = StatisticViewHolder(
        ItemStatisticSellingBinding.inflate(parent.context.inflater, parent, false)
    )

    override fun onBindViewHolder(holder: StatisticViewHolder, position: Int) {
        holder.bind(position)
    }

    inner class StatisticViewHolder(
        private val binding: ItemStatisticSellingBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(position: Int) = with(binding) {
            val salesValue = salesRange?.get(position)
            tvSalesReport.text = salesOptions[position]
            tvSalesPrice.text = salesValue.orEmpty()
            root.setOnClickListener { itemClickListener?.invoke(bindingAdapterPosition) }
        }
    }

    object StatisticDiffCallback : DiffUtil.ItemCallback<String>() {
        override fun areItemsTheSame(oldItem: String, newItem: String): Boolean {
            return oldItem == newItem
        }

        override fun areContentsTheSame(oldItem: String, newItem: String): Boolean {
            return oldItem == newItem
        }
    }
}

Log.d("ProfileStatistics salesRangeLiveData","salesRange 值: "+salesRange) -> salesRange 值: []

个人资料统计活动

@AndroidEntryPoint
class ProfileStatisticsActivity: AppCompatActivity() {
     private val binding by viewBinding(ActivityProfileStatisticsBinding::inflate)

    private val viewModel: ProfileStatisticsViewModel by viewModels()

    private val statisticAdapter: ProfileStatisticAdapter by lazy {
        ProfileStatisticAdapter(emptyList())
    }
     override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        collectState(viewModel.uiState) { renderView(it) } ...
     
 private fun renderView(uiState: StatisticsUiState) = with(binding) {

        viewModel.userLiveData.value.apply {
            tvUserName.text = this?.userName.orEmpty()
        }

        statisticAdapter.submitList(uiState.salesRange)
        statisticAdapter.notifyDataSetChanged()
    }

statisticAdapter.submitList(uiState.salesRange)
这里的 uiState.salesRange 值一开始为 null,然后用服务中的数据填充。

查看模型:

     private val _uiState = MutableStateFlow(StatisticsUiState())
    val uiState: StateFlow<StatisticsUiState> = _uiState.asStateFlow()

init {
        userLiveData.value?.id?.let {
            fetchStatistics(it)
        }
    }

  private fun fetchStatistics(userId: Int) {
        viewModelScope.launch {
            runCatching { fetchStatistics.invoke(userId) }
                .onSuccess {statistics ->
                    _uiState.update {
                        it.copy(
                            salesRange = statistics.sales?.range
                        )
                    }
                    userStatisticsLiveData.value = statistics
                    salesRangeLiveData.value = statistics.sales?.range
                }.onFailure { showError(it) }

        }
    }

UiState:

data class StatisticsUiState(
    val userStatistics: StatisticsResponse?= null,
    val salesRange: List<String>? = null
)
android adapter android-livedata
1个回答
0
投票

已解决:

ProfileStatisticAdapter

class ProfileStatisticAdapter(
) : ListAdapter<String, ProfileStatisticAdapter.StatisticViewHolder>(StatisticDiffCallback) {

    var salesRange = listOf<Float>() 
    ...
  @SuppressLint("NotifyDataSetChanged")
    fun updateItems(salesRange: List<Float>) {
        if (salesRange != this.salesRange) {
            this.salesRange = salesRange
            notifyDataSetChanged()
        }
    }
    ...

活动:

 statisticAdapter.updateItems(userStatistics.sales?.range.orEmpty())

我按照上面的方法解决了这个问题。我在 ProfileStatisticAdapter 中声明了默认列表,并添加了更新函数和活动句柄。

© www.soinside.com 2019 - 2024. All rights reserved.