Kotlin(anko)使用回收者视图将主要活动中的数据解析为另一个活动[CLOSE]

时间:2018-11-01 07:38:50

标签: android kotlin anko

我想让回收站视图在点赞商品时解析我的数组数据

strings.xml

<string-array name="club_name">
        <item>Barcelona FC</item>
        <item>Real Madrid FC</item>
        <item>Bayern Munchen FC</item>
        <item>Manchester City FC</item>
        <item>Manchester United FC</item>
        <item>Chelsea FC</item>
        <item>AC Milan FC</item>
        <item>Arsenal FC</item>
    </string-array>

    <array name="club_image">
        <item>@drawable/img_barca</item>
        <item>@drawable/img_madrid</item>
        <item>@drawable/img_bayern</item>
        <item>@drawable/img_city</item>
        <item>@drawable/img_mu</item>
        <item>@drawable/img_chelsea</item>
        <item>@drawable/img_acm</item>
        <item>@drawable/img_arsenal</item>
    </array>
    <array name="desc">
        <item>asdfghjkl</item>
        <item>zxcvbnm</item>
    </array>

MainActivity.kt

    class MainActivity : AppCompatActivity() {


    private var  items : MutableList<Item> = mutableListOf()

    private fun initData(){
        val name = resources.getStringArray(R.array.club_name)
        val image = resources.obtainTypedArray(R.array.club_image)
        items.clear()
        for (i in name.indices){
            items.add(Item(name[i],image.getResourceId(i,0),""))
        }
        image.recycle()
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initData()
        club_list.layoutManager = LinearLayoutManager(this)
        club_list.adapter = RecyclerViewAdapter(this,items){
            val toast = Toast.makeText(applicationContext,it.name,Toast.LENGTH_SHORT)
            toast.show()
        }
    }
}

RecyclerAdapter.kt

class RecyclerViewAdapter(private val context: Context,private val items: List<Item>,private  val listener:(Item)->Unit)
    : RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>() {

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindItem(items[position],listener)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
        ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_list, parent, false))


    override fun getItemCount(): Int = items.size

    class ViewHolder(override val containerView: View) :RecyclerView.ViewHolder(containerView),LayoutContainer {

        fun bindItem(items:Item,listener: (Item) -> Unit){
            name.text = items.name
            items.image?.let { Picasso.get().load(it).into(image)}
            containerView.setOnClickListener{
                listener(items)
            }
        }

    }
}

这是Item是数据类 Item.kt

data class Item(val name:String?,val image:Int?,val desc: String?)

所以我要在mainactivity中显示数据俱乐部名称和俱乐部图像的数组,但我想将数据发送到detailactivity,其中显示名称,图像和描述

1 个答案:

答案 0 :(得分:0)

您可以通过context.getStringArrayMethod

来获取字符串数组。

如果这是适配器,则需要上下文:

var yourVar = context.getStringArray(R.array.your_string_array)

如果这是活动,则可以直接访问getSTringArray

var yourVar = context.getStringArray(R.array.your_string_array)