简体   繁体   English

将上下文转换为活动以便在回收视图中使用? (科特琳)

[英]Typecast context into Activity for use in Recycle View? (Kotlin)

I can send data from activity to recyclerview adapter file without any problems. 我可以将数据从活动发送到recyclerview适配器文件,而不会出现任何问题。

Typecast context into Activity 将上下文转换为活动

For example, I can type 例如,我可以输入

var x:Int = 9000 var x:Int = 9000

in MainActivity file and then I go to RecyclerAdapter and simply type 在MainActivity文件中,然后转到RecyclerAdapter并简单地键入

var y:Int = MainActivity().x var y:Int = MainActivity()。x

to have RecyclerAdapter fetch it and use it to preform its operations. 让RecyclerAdapter提取它并使用它执行其操作。

I can't actually do the same, transferring data from RecyclerAdapter() to MainActivity(). 我实际上不能做同样的事情,将数据从RecyclerAdapter()传输到MainActivity()。 I tried 我试过了

var x:Int= 9000 var x:Int = 9000

in RecyclerAdapter() and then 在RecyclerAdapter()中,然后

var y:Int= RecyclerAdapter().x var y:Int = RecyclerAdapter()。x

inside MainActivity but the app doesn't fetch the data. 在MainActivity内部,但应用程序未获取数据。

What should I do? 我该怎么办? I already tried googling for solutions but most were solutions to complex code. 我已经尝试使用谷歌搜索解决方案,但是大多数都是复杂代码的解决方案。 My actual app is also pretty complex but I would like to have a sample solution to this simple problem then work my way up from there onward. 我的实际应用程序也非常复杂,但是我想为这个简单的问题提供一个示例解决方案,然后从此继续前进。

Edit: Sorry I got the naming of the variables wrong. 编辑:对不起,我弄错了变量的命名。 I corrected it now. 我现在纠正了。

You can do it like this.... 你可以这样

 val activity = context as ExampleActivity
 var i=activity.getCount()

In your Activity Class. 在您的活动班级中。

class ExampleActivity: AppCompatActivity() {


    private var count = 0

     override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_example)
        var mList = findViewById(R.id.list) as RecyclerView
        var mAdapter = CustomAdapter(this);
        mList.layoutManager = LinearLayoutManager(this)
        mList.adapter = mAdapter
      }

    fun setCount(i: Int) {
        count = i
    }

    fun getCount(): Int {
        return count
    }
}

In Your AdapterClass. 在您的AdapterClass中。

class CustomAdapter(var context:Context) : RecyclerView.Adapter<CustomHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomHolder {
        return CustomHolder(LayoutInflater.from(context).inflate(R.layout.cell,parent,false))
    }

    override fun getItemCount(): Int {
        return 10;
    }

    override fun onBindViewHolder(holder: CustomHolder, position: Int) {

        val activity = context as ExampleActivity
        var i=activity.getCount()
         i+=20
        activity.setCount(i)
    }


}

class CustomHolder(val item: View) : RecyclerView.ViewHolder(item) {

}

This will provide you Abstraction Functionality of Classes . 这将为您提供类的抽象功能

Hope you understand by this..... 希望您对此有所了解.....

Nevermind. 没关系。 I got a workaround. 我有一个解决方法。 Posting here for anyone who wants an answer for reference. 张贴在这里给想要答案的人。

Create an Object kotlin file, then pass data from recycler adapter file to Object, then from Object to MainActivity 创建一个对象kotlin文件,然后将数据从回收站适配器文件传递到对象,然后从对象传递到MainActivity

Example: In recycler adapter file: Object.x = x 示例:在回收站适配器文件中:Object.x = x

Then in MainActivity(): x = Object.x 然后在MainActivity()中:x = Object.x

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

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