adsl9002 2022-10-05 13:35 采纳率: 46.2%
浏览 69
已结题

recycter点击的数据怎么显示在另一recycter

2个recycterview,类似点餐的情形。其中一个recycter添加了4个texiview,有品类和价格2个参数,点击哪个TextView就把参数显示在另一个recycter中。

我在TextAdapter的点击事件中向drinkList添加数据,不过报错行不通。
零基础自学,还有很多不懂的,希望能详细的指点。

MainActivity.kt

package android.example.drink

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity(){
    private val textList = ArrayList<PinLei>()
    public val drinkList = ArrayList<Drinks>()
    public lateinit var driAdapter: DrinkAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val recyclerDrink: RecyclerView = findViewById(R.id.recyclerView)
        recyclerDrink.layoutManager = LinearLayoutManager(this)
        driAdapter = DrinkAdapter(drinkList)
        recyclerDrink.adapter = driAdapter

        val recyclerText: RecyclerView = findViewById(R.id.reCycTextView)
        recyclerText.layoutManager = GridLayoutManager(this,3)
        val textTer = TextAdapter(textList)
        recyclerText.adapter = textTer
        initText()
    }

    private fun initText() {
        textList.add(PinLei("可乐", 8))
        textList.add(PinLei("奶茶", 10))
        textList.add(PinLei("冰红茶", 2))
        textList.add(PinLei("+冰块", 3))
    }
}

Drinks.kt

package android.example.drink

class Drinks(var name: String, var price: Int)

Pinlei.kt

package android.example.drink

class PinLei(val pn1: String, val price: Int)

DrinkAdapter.kt

package android.example.drink

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class DrinkAdapter(private val drinkList: List<Drinks>) :
    RecyclerView.Adapter<DrinkAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val xinXi1: TextView = view.findViewById(R.id.text1)
        val xinXi2: TextView = view.findViewById(R.id.text2)
    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val drink = drinkList[position]
        holder.xinXi1.text = drink.name
        holder.xinXi2.text = drink.price.toString()
    }

    override fun getItemCount() = drinkList.size
}

TextAdapter.kt

package android.example.drink

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class TextAdapter(private val textList: List<PinLei>) :
    RecyclerView.Adapter<TextAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val pinLei: TextView = view.findViewById(R.id.typeBt)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.pinlei_ltem, parent, false)
        val viewHolder = ViewHolder(view)


        viewHolder.pinLei.setOnClickListener {
            val position = viewHolder.bindingAdapterPosition
            val text = textList[position]
            MainActivity().drinkList.add(Drinks(text.pn1, text.price) )
            MainActivity().driAdapter.notifyItemInserted(MainActivity().drinkList.size -1)
        }
        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val drink = textList[position]
        holder.pinLei.text = drink.pn1
    }

    override fun getItemCount() = textList.size
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d8e0e8"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toTopOf="@+id/reCycTextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="1">
    </androidx.recyclerview.widget.RecyclerView>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/reCycTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="152dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent">
    </androidx.recyclerview.widget.RecyclerView>

</androidx.constraintlayout.widget.ConstraintLayout>

drink_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:textSize="24sp"
        android:textColor="@color/black"/>

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:textSize="24sp"
        android:textColor="@color/black"/>

</androidx.appcompat.widget.LinearLayoutCompat>

pinlei_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="16dp">

    <Button
        android:id="@+id/typeBt"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:textSize="24sp"
        android:backgroundTint="#686868"
        />

</LinearLayout>
运行结果及报错内容

img

  • 写回答

2条回答 默认 最新

  • 寒门亭 2022-10-12 11:58
    关注

    不清楚为啥你的driAdapter没有实例化 建议是在适配器中通过接口把数据抛出去处理 而不是在适配器中处理

    class TextAdapter(private val textList: List<PinLei> ,val click:OnTextItemClick) :
        RecyclerView.Adapter<TextAdapter.ViewHolder>() {
    
        inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            val pinLei: TextView = view.findViewById(R.id.typeBt)
        }
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.pinlei_item, parent, false)
            return ViewHolder(view)
        }
    
        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val drink = textList[position]
            holder.pinLei.text = drink.pn1
            holder.pinLei.setOnClickListener {
                val position = holder.position
                val text = textList[position]
                click.click(pn1 =text.pn1 , price =   text.price)
    //            MainActivity().drinkList.add(Drinks(text.pn1, text.price) )
    //            MainActivity().driAdapter?.notifyItemInserted(MainActivity().drinkList.size -1)
            }
        }
    
        override fun getItemCount() = textList.size
        interface  OnTextItemClick{
            fun click(pn1 :String,price:Int)
        }
    
    }
    //MainActivity
    val recyclerText: RecyclerView = findViewById(R.id.reCycTextView)
            recyclerText.layoutManager = GridLayoutManager(this, 3)
            val textTer = TextAdapter(textList,object :TextAdapter.OnTextItemClick{
                override fun click(pn1: String, price: Int) {
                    drinkList.add(Drinks(pn1 ,price))
                    driAdapter?.notifyDataSetChanged()
                }
            })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
    1人已打赏
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月21日
  • 已采纳回答 10月13日
  • 创建了问题 10月5日

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号