这样传递 ID 是正确的吗?我不明白这个错误。

huangapple go评论83阅读模式
英文:

Is it right how to pass the id ? I didn't understand the error?

问题

/*****这里是我的列表适配器的部分*****/

class CurrencyMenuAdapter(
    var viewModel: MainViewModel,
    var currencyList: ObservableList<CurrencyRoomItem>
) : ObservableRecyclerViewAdapter<CurrencyRoomItem, BaseViewHolder<CurrencyRoomItem>>(currencyList) {
    private val TAG = "CurrencyMenuAdapter"
    private val model = MainViewModel()

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BaseViewHolder<CurrencyRoomItem> {
        Log.e(TAG, "onCreateViewHolder: 1")
        return Holder(
            ItemcurrencymenuBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(
        holder: BaseViewHolder<CurrencyRoomItem>,
        position: Int
    ) {
        Log.e(TAG, "onCreateViewHolder: 1")
        holder.bind(currencyList[position])
    }

    inner class Holder(private val binding: ItemcurrencymenuBinding) :
        BaseViewHolder<CurrencyRoomItem>(binding.root) {
        override fun bind(item: CurrencyRoomItem) {
            binding.item = item
            binding.btnCurrency.setOnClickListener{
                viewModel.goToRoom(item.id!!)
                Paper.book().write(Const.SAVE_ID, item.id)
            }
        }
    }
}
/*********************************************************************************************/


/*****这里是ViewModel部分*****/

class MainViewModel : BaseViewModel<MainNavigator>() {
    var firstName = ObservableField<String>()
    var lastName = ObservableField<String>()
    var userEmail = ObservableField<String>()
    var phoneNumber = ObservableField<String>()
    var listRoom = ObservableArrayList<CurrencyRoomItem>()
    lateinit var id: String
    private var useCase = GetCurrenciesMenuUseCase()

    init {
    }

    fun goToRoom(groupId: String) {
        getNavigator()!!.town(groupId)
    }
}
/*****ViewModel部分*****/


/*****这里是Activity部分*****/

class MainActivity : BaseActivity<ActivityHomeBinding>(), MainNavigator {
    private lateinit var model: MainViewModel
    private lateinit var alertDialog: AlertDialog
    var dialog: Dialog? = null

    override fun hideLogoutDialog() {
        alertDialog.dismiss()
    }

    var loginResponse = ObservableField<LoginResponse>()

    override fun town(groupId: String) {
        startActivity(Intent(this@MainActivity, CurrencyRoomActivity::class.java))
        finish()
    }
}
/*****Activity部分*****/


/****这里是BaseViewModel部分****/

abstract class BaseViewModel<N> : ViewModel() {
    private var mCompositeDisposable: CompositeDisposable = CompositeDisposable()
    private lateinit var mNavigator: WeakReference<N>

    fun getNavigator(): N? {
        return mNavigator?.get()
    }

    fun setNavigator(navigator: N) {
        this.mNavigator = WeakReference(navigator)
    }

    override fun onCleared() {
        mCompositeDisposable.dispose()
        super.onCleared()
    }

    fun getCompositeDisposable(): CompositeDisposable {
        return mCompositeDisposable
    }
}
/****BaseViewModel部分****/


/******错误信息******/
// 这里是错误截图的链接
[Error][1]
[1]: https://i.stack.imgur.com/955oy.png
/******错误信息******/
英文:
/*****here the Adapter of my list *****/
class CurrencyMenuAdapter(
var viewModel : MainViewModel,
var currencyList: ObservableList&lt;CurrencyRoomItem&gt;) : ObservableRecyclerViewAdapter&lt;CurrencyRoomItem, BaseViewHolder&lt;CurrencyRoomItem&gt;&gt;(currencyList){
private val TAG = &quot;CurrencyMenuAdapter&quot;
private val model = MainViewModel()
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): BaseViewHolder&lt;CurrencyRoomItem&gt; {
Log.e(TAG, &quot;onCreateViewHolder: 1&quot;)
return Holder(
ItemcurrencymenuBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)
}
override fun onBindViewHolder(
holder: BaseViewHolder&lt;CurrencyRoomItem&gt;,
position: Int
) {
Log.e(TAG, &quot;onCreateViewHolder: 1&quot;)
holder.bind(currencyList[position])
}
inner class Holder(private val binding:ItemcurrencymenuBinding) :
BaseViewHolder&lt;CurrencyRoomItem&gt;(binding.root) {
override fun bind(item: CurrencyRoomItem) {
binding.item = item
binding.btnCurrency.setOnClickListener{
viewModel.goToRoom(item.id!!)
Paper.book().write(Const.SAVE_ID, item.id)
}
}
}

}
//
/
/
/*********************************************************************************************/

/Here the ViewModel/

class MainViewModel:BaseViewModel<MainNavigator>() {

var firstName = ObservableField&lt;String&gt;()
var lastName = ObservableField&lt;String&gt;()
var userEmail = ObservableField&lt;String&gt;()
var phoneNumber = ObservableField&lt;String&gt;()
var listRoom= ObservableArrayList&lt;CurrencyRoomItem&gt;()
lateinit var id :String
private var useCase = GetCurrenciesMenuUseCase()
init {
}
fun goToRoom(groupId:String){
getNavigator()!!.town(groupId)
}

/ViewModel/

/***** here the Activity*****/

class MainActivity : BaseActivity<ActivityHomeBinding>(), MainNavigator {

private lateinit var model: MainViewModel
private lateinit var alertDialog: AlertDialog
var dialog: Dialog? = null
override fun hideLogoutDialog() {
alertDialog.dismiss()
}
var loginResponse = ObservableField&lt;LoginResponse&gt;()
override fun town(groupId: String) {
startActivity(Intent(this@MainActivity, CurrencyRoomActivity::class.java))
finish()
}

/Activity/

/here the BaseViewModel/

abstract class BaseViewModel&lt;N&gt; : ViewModel() {
private  var mCompositeDisposable: CompositeDisposable = CompositeDisposable();
private lateinit var mNavigator: WeakReference&lt;N&gt;
fun getNavigator(): N? {
return mNavigator?.get()
}
fun setNavigator(navigator: N) {
this.mNavigator = WeakReference(navigator)
}
override fun onCleared() {
mCompositeDisposable.dispose()
super.onCleared()
}
fun getCompositeDisposable(): CompositeDisposable {
return mCompositeDisposable
}

}

/BaseViewModel/

/Erorr/

//here the erorr
Error

/Erorr/

答案1

得分: 1

错误的意思是在 mNavigator 获得值之前就使用了它,在使用 "lateinit" 时,你“承诺”永远不会出现这种情况。

在调用 setNavigator 之前调用了 getNavigator(实际上你从未调用过 setNavigator)。

要解决这个问题,你只需在调用 getNavigator 之前调用 setNavigator。这将解决你的具体问题,但 getNavigator 和 setNavigator 似乎有点奇怪,也许考虑在构造函数中进行初始化。

编辑:

我猜你需要将 MainActivity 传递给 setNavigator,因为它扩展了 MainNavigator。在这种情况下,只需在 MainActivity 中使用 ViewModel 变量,并调用:

viewModel.setNavigator(this)

MainActivity 扩展了 MainNavigator,而 MainViewModel 扩展了 BaseViewModel,所以应该可以正常工作。

英文:

The error means that mNavigator is used before it has a value, by saying lateinit you "promise" that that is never the case.

You call getNavigator before calling setNavigator though (you never call setNavigator)

To fix it you just have to call setNavigator before calling getNavigator. That would fix your specific problem but getNavigator and setNavigator seem a bit weird, perhaps consider initializing it in the constructor

Edit:

I guess you need to pass MainActivity as it extends MainNavigator to setNavigator. In that case just use the ViewModel variable in MainActivity and call

viewModel.setNavigator(this)

MainActivity extends MainNavigator and MainViewModel extends BaseViewModel so that should work

huangapple
  • 本文由 发表于 2020年10月26日 05:49:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64529183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定