在不同的KT文件中获取不同KT文件上的ID iD by ID,以设置映像

发布于 2025-02-11 14:41:18 字数 4854 浏览 1 评论 0 原文

因此,基本上,我正在进行一项活动,其中在按钮中单击屏幕上的一个警报对话框显示,其中包含带卡视图和一个关闭按钮的Recyclerview。现在,通过单击“卡片视图”,我已经成功敬酒了一条消息,但是现在需要获取cardView的映像,然后将其设置为不同XML文件的imageView(该cardView也位于不同的KT文件中)。简而言之,主要问题是我找不到该图像视图中的不同KT文件。
因此,我尝试了下面提到的几种方法

  1. 第一种方法 - &gt; view.findviewbyid (在这种情况下, error is <强> view.findViewById(r.id.custom_image_view)不得为null
  2. 第二种方法 - &gt; 使用 view.find.find (在这种情况下, error is null不能被施加到非零类型android.widget.imageview
  3. 第三种方法 - &gt; 使用 className()。imageView lateInit属性没有初始化 )方法,但问题仍然存在。

因此,下面的代码包含用于显示警报对话框的按钮。
代码文件,名为 createCustomAgageactivity.kt

class CreateCustomImageActivity : AppCompatActivity(), View.OnTouchListener {

    /* some code */

    lateinit var imageView: ImageView
    
    /* some code */
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_create_custom_image)

        /* below is the imageView in which I want to set image */

        imageView = findViewById(R.id.custom_image_view)

        /* below is the imageButton on which by clicking alert dialog with recyclerView
           with cardView is displaying */

        val image = findViewById<ImageButton>(R.id.bg_image)

        image.setOnClickListener {
            val selectImageDialog = LayoutInflater.from(this)
                .inflate(R.layout.activity_choose_background_image_list, null)
            val builder = AlertDialog.Builder(this).setView(selectImageDialog)
            val showDialog = builder.show()
            showDialog.window?.setGravity(Gravity.BOTTOM)

            val close = selectImageDialog.findViewById<Button>(R.id.choose_image)
            val recyclerImageList = selectImageDialog.findViewById<RecyclerView>(R.id.image_list_recycler_view)

            val imageCard = ArrayList<backgroundImageListDatabase>()
            imageCard.add(backgroundImageListDatabase(R.drawable.o))
            imageCard.add(backgroundImageListDatabase(R.drawable.t))
            imageCard.add(backgroundImageListDatabase(R.drawable.th))
            imageCard.add(backgroundImageListDatabase(R.drawable.f))
            imageCard.add(backgroundImageListDatabase(R.drawable.fi))
            imageCard.add(backgroundImageListDatabase(R.drawable.s))
            imageCard.add(backgroundImageListDatabase(R.drawable.se))

            recyclerImageList.setLayoutManager(
                GridLayoutManager(this,2,
                    GridLayoutManager.VERTICAL,false)
            )
            val adapter = BackgroundImageCardAdapter(imageCard)
            recyclerImageList.setAdapter(adapter)

            close.setOnClickListener {
                showDialog.dismiss()
            }
        }
    }
}

和下面代码代码包含用于绑定cardView to recyclerview的适配器类。
代码文件称为 peactionbackgroundImmageListactivity.kt

class BackgroundImageCardAdapter(val backgroundImageCardDetails: ArrayList<backgroundImageListDatabase>) :
        RecyclerView.Adapter<BackgroundImageCardAdapter.ViewHolder>() {

            class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

//                var bgImageView: ImageView = view.find(R.id.custom_image_view)

                fun bindItems(details: backgroundImageListDatabase) {
                    val imageView = itemView.findViewById<ImageView>(R.id.background_image_card_image)

                    imageView.setImageResource(details.image)

                    imageView.setOnClickListener {

//                        bgImageView.setImageResource(details.image)

                        Toast.makeText(imageView.context,details.image.toString(),Toast.LENGTH_SHORT).show()
                        Log.d(" - CARD CLICK EVENT - ","Clicked")
                    }
                }

            }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val cardView: View = LayoutInflater.from(parent.context).inflate(R.layout.choose_background_image_card,parent,false)
        return ViewHolder(cardView)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindItems(backgroundImageCardDetails[position])

//        holder.itemView.setOnClickListener {
//            Toast.makeText(holder.itemView.context,"CARD CLICKED",Toast.LENGTH_SHORT).show()
//            Log.d(" - CARD CLICK EVENT - ","Clicked")
//        }
    }

    override fun getItemCount(): Int {
        return backgroundImageCardDetails.size
    }
}

预先感谢。

So basically I'm making an activity in which in button click one alert dialog displays on screen and it contain recyclerview with cardview and one close button. now, by clicking on cardview I have successfully toast a message but now there is requirement to get the image of cardview and then set it into imageview of different xml file (that cardview is also in different kt file). In short the main problem is i can not find that imageview into that different kt file.

So, I have tried few approaches mentioned below

  1. First Approach -> view.findViewById (in this case the error is view.findViewById(R.id.custom_image_view) must not be null)
  2. Second Approach -> by using view.find (in this case the error is null cannot be cast to non-null type android.widget.ImageView)
  3. Third Approach -> accessing using className().imageview (in this case the error is lateinit property is not been initialized) method but the problem is still there.

So, below code contains button for displaying alert dialog.
code file named as CreateCustomImageActivity.kt

class CreateCustomImageActivity : AppCompatActivity(), View.OnTouchListener {

    /* some code */

    lateinit var imageView: ImageView
    
    /* some code */
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_create_custom_image)

        /* below is the imageView in which I want to set image */

        imageView = findViewById(R.id.custom_image_view)

        /* below is the imageButton on which by clicking alert dialog with recyclerView
           with cardView is displaying */

        val image = findViewById<ImageButton>(R.id.bg_image)

        image.setOnClickListener {
            val selectImageDialog = LayoutInflater.from(this)
                .inflate(R.layout.activity_choose_background_image_list, null)
            val builder = AlertDialog.Builder(this).setView(selectImageDialog)
            val showDialog = builder.show()
            showDialog.window?.setGravity(Gravity.BOTTOM)

            val close = selectImageDialog.findViewById<Button>(R.id.choose_image)
            val recyclerImageList = selectImageDialog.findViewById<RecyclerView>(R.id.image_list_recycler_view)

            val imageCard = ArrayList<backgroundImageListDatabase>()
            imageCard.add(backgroundImageListDatabase(R.drawable.o))
            imageCard.add(backgroundImageListDatabase(R.drawable.t))
            imageCard.add(backgroundImageListDatabase(R.drawable.th))
            imageCard.add(backgroundImageListDatabase(R.drawable.f))
            imageCard.add(backgroundImageListDatabase(R.drawable.fi))
            imageCard.add(backgroundImageListDatabase(R.drawable.s))
            imageCard.add(backgroundImageListDatabase(R.drawable.se))

            recyclerImageList.setLayoutManager(
                GridLayoutManager(this,2,
                    GridLayoutManager.VERTICAL,false)
            )
            val adapter = BackgroundImageCardAdapter(imageCard)
            recyclerImageList.setAdapter(adapter)

            close.setOnClickListener {
                showDialog.dismiss()
            }
        }
    }
}

And below code contains the Adapter class for binding cardView to recyclerView.
Code file named as ChooseBackgroundImageListActivity.kt

class BackgroundImageCardAdapter(val backgroundImageCardDetails: ArrayList<backgroundImageListDatabase>) :
        RecyclerView.Adapter<BackgroundImageCardAdapter.ViewHolder>() {

            class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

//                var bgImageView: ImageView = view.find(R.id.custom_image_view)

                fun bindItems(details: backgroundImageListDatabase) {
                    val imageView = itemView.findViewById<ImageView>(R.id.background_image_card_image)

                    imageView.setImageResource(details.image)

                    imageView.setOnClickListener {

//                        bgImageView.setImageResource(details.image)

                        Toast.makeText(imageView.context,details.image.toString(),Toast.LENGTH_SHORT).show()
                        Log.d(" - CARD CLICK EVENT - ","Clicked")
                    }
                }

            }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val cardView: View = LayoutInflater.from(parent.context).inflate(R.layout.choose_background_image_card,parent,false)
        return ViewHolder(cardView)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindItems(backgroundImageCardDetails[position])

//        holder.itemView.setOnClickListener {
//            Toast.makeText(holder.itemView.context,"CARD CLICKED",Toast.LENGTH_SHORT).show()
//            Log.d(" - CARD CLICK EVENT - ","Clicked")
//        }
    }

    override fun getItemCount(): Int {
        return backgroundImageCardDetails.size
    }
}

Thanks in Advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温折酒 2025-02-18 14:41:18

它不是将它们放入相同或不同的KT文件中,而是您无法访问任何视图,直到您具有参考为止。
我的意思是,您仅在对话框在屏幕上时只能访问imageview,并且可以使用 selectimagedialog.findviewbyid 访问其值,

此处的最佳选择是使用回调(当您单击贴图中的图像中时适配器的父母应该注意到单击新图像)

首先创建回调:

interface CardAdapterCallback {
    fun onImageClicked(resId : Int)
}

然后更改适配器构造函数并添加回调:

class BackgroundImageCardAdapter(
    val backgroundImageCardDetails: ArrayList<backgroundImageListDatabase>,
    val callback : CardAdapterCallback
) :RecyclerView.Adapter<BackgroundImageCardAdapter.ViewHolder>() {...}

然后,当您在适配器Binditems binditems函数中单击图像时,将您的回调发送给您的活动:

fun bindItems(details: backgroundImageListDatabase) {
     val imageView = itemView.findViewById<ImageView>(R.id.background_image_card_image)

    imageView.setImageResource(details.image)
    imageView.setOnClickListener {
        callback.onImageClicked(details.image) 
    }
}

更新

更改活动中创建的适配器:(

val adapter = BackgroundImageCardAdapter(
    imageCard,
    object : CardAdapterCallback {
         override fun onImageClicked(resId : Int){
             imageView.setImageResource(resId)
        }
    })

对象:cardAdapterCallback {}是为了解决您的参数错误)

,并修复Binditems函数中的回调变量错误在您的背景ImmageCardAdapter中更改此行:

inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    fun bindItems(details: backgroundImageListDatabase) {
        val imageView = itemView.findViewById<ImageView>(R.id.background_image_card_image)

        imageView.setImageResource(details.image)
        imageView.setOnClickListener {
            callback.onImageClicked(details.image) 
        }
    }
}

使用 inner 关键字,您将能够在您的binditems函数中呼叫 callback.onimageclicked(lideses.image)

Its not about putting them in the same or different kt file, You cant access any view until you have its reference.
I mean you have only access to your imageView when your dialog is on the screen and you can access its value with selectImageDialog.findViewById

The best option here is using Callbacks(when you click on a image in your adapter the parent of your adapter should get noticed that new image is clicked)

first create your callback:

interface CardAdapterCallback {
    fun onImageClicked(resId : Int)
}

then change your adapter constructor and add your callback:

class BackgroundImageCardAdapter(
    val backgroundImageCardDetails: ArrayList<backgroundImageListDatabase>,
    val callback : CardAdapterCallback
) :RecyclerView.Adapter<BackgroundImageCardAdapter.ViewHolder>() {...}

then when you click on a image in your adapter bindItems function send your callback to your activity:

fun bindItems(details: backgroundImageListDatabase) {
     val imageView = itemView.findViewById<ImageView>(R.id.background_image_card_image)

    imageView.setImageResource(details.image)
    imageView.setOnClickListener {
        callback.onImageClicked(details.image) 
    }
}

Updated

change your adapter creating in your activity to this:

val adapter = BackgroundImageCardAdapter(
    imageCard,
    object : CardAdapterCallback {
         override fun onImageClicked(resId : Int){
             imageView.setImageResource(resId)
        }
    })

(object:CardAdapterCallback{} is for fixing your parameter error)

And to fix your callback variable error in bindItems function change this line in your BackgroundImageCardAdapter:

inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    fun bindItems(details: backgroundImageListDatabase) {
        val imageView = itemView.findViewById<ImageView>(R.id.background_image_card_image)

        imageView.setImageResource(details.image)
        imageView.setOnClickListener {
            callback.onImageClicked(details.image) 
        }
    }
}

with that inner keyword, you will be able to call callback.onImageClicked(details.image) in your bindItems function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文