简体   繁体   English

Kotlin中需要ViewHolder getter / setter中的对象

[英]Object in ViewHolder getter/setter required on Kotlin

not sure what is going on. 不知道发生了什么事。 This worked fine in Java but when switching to Kotlin I don't really know how to make this work. 这在Java中运行良好,但是当切换到Kotlin时,我真的不知道如何使这项工作。 I get this error But I get the same error which is 我得到这个错误但是我得到了同样的错误

Property getter or setter required on the val mMoment: Moment val mMoment Property getter or setter required :瞬间

class SquareViewHolder(v: CardView, viewModel: BarreViewModel) : RecyclerView.ViewHolder (v) {

    val mImage: ImageView
    val by lazy mMoment: Moment


    init {

        mImage = v.findViewById(R.id.square_moment)

        mImage.setOnClickListener {
            //val focused = ArrayList<Moment>(0)
            //focused.add(mMoment)

            //viewModel.focusedMoments = focused
            //viewModel.setItemClicked(true)
        }
    }



    fun bind(moment: Moment) {
        val mMoment = moment
    }
}

I have also tried var lateinit mMoment: Moment But I receive the same error. 我也试过var lateinit mMoment: Moment但是我收到同样的错误。

Now, the moment object is a Kotlin Data Object, I'm not sure if that would cause a problem. 现在,moment对象是Kotlin数据对象,我不确定这是否会导致问题。

This worked on java, but for some reason kotlin doesn't seem to like the instantiation of the object this way. 这适用于java,但由于某种原因,kotlin似乎不喜欢这种方式对象的实例化。 I don't know why. 我不知道为什么。 Thanks for any help! 谢谢你的帮助!

In Java it was: 在Java中它是:

public class SquareMomentViewHolder extends RecyclerView.ViewHolder {

public ImageView mImage;
public Moment mMoment;

BarreChatViewModel viewModel;

public SquareMomentViewHolder(CardView v, BarreChatViewModel viewModel) {
    super(v);
    mImage = v.findViewById(R.id.square_moment);

    mImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            List<Moment> focused = new ArrayList(0);
            focused.add(mMoment);
            viewModel.setFocusedMoments(focused);

            viewModel.setItemClicked(true);
        }
    });
}

public void bind(Moment moment) {
    mMoment = moment;
}

}

A possible cause of this error might be that this dataclass is used for my ROOMDatabase. 导致此错误的可能原因可能是此数据类用于我的ROOMDatabase。 It's an entity object I was dragging to the User Interface. 这是我拖动到用户界面的实体对象。 Maybe this isn't a proper way to do things. 也许这不是一种正确的做事方式。 I don't know. 我不知道。

Lazy is where it will initialise the variable only when it is used, but it requires some code when declaring it as lazy. Lazy是仅在使用变量时才初始化变量的地方,但在将变量声明为惰性时需要一些代码。

Eg val mMoment by lazy { // Some code that generates this } 例如, val mMoment by lazy { // Some code that generates this }

As your variable will be initialised in a bind, you should do: 由于您的变量将在绑定中初始化,您应该:

lateinit var mMoment: Moment

This means that it will get initialised later on. 这意味着它将在稍后初始化。 If you try to use it without it being initialised, your program will throw an exception. 如果您尝试在没有初始化的情况下使用它,您的程序将抛出异​​常。

Or if this will potentially never get bound, you should initialise it as a nullable value: 或者,如果这可能永远不会被绑定,您应该将其初始化为可以为空的值:

var mMoment: Moment? = null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM