如何在片段中用适配器数据填充我的recyclerview?

问题描述 投票:0回答:1

所以我现在已经在互联网上搜索了一段时间,但是我似乎找不到合适的主题来帮助我解决这个问题。我有以下与此问题相关的代码:

这是我的适配器类。

class SmoelenBoekAdapter(var profiles: Array<Profile>) : RecyclerView.Adapter<CustomViewHolder>(){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val layout = layoutInflater.inflate(com.otten.nvvofrankversie.R.layout.recyclerview_smoelenboek, parent, false)
        return CustomViewHolder(layout)
    }

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
        val positioner = profiles.get(position)
        var profileImage = positioner.profile_image
        var firstName = positioner.first_name
        var lastName = positioner.last_name
        var plaats = positioner.place
        var adres = positioner.address
        holder.view.naamSmoelenBoek.text = firstName.string + " " + lastName.string
    }

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

class CustomViewHolder(val view: View) : RecyclerView.ViewHolder(view)

这是我的'MainActivity'(针对此特定片段)

class SmoelenBoek : ApplicationFragment(){
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_smoelenboek, null)
    }

    /*override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        smoelenboekRecyclerView.adapter = SmoelenBoekAdapter(profiles)
    }*/
}

xml其中必须填写adapterinfo:

<?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="wrap_content"
    android:orientation="vertical">


    <ImageView
        android:id="@+id/profielPicSmoelenBoek"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/business_icon" />

    <TextView
        android:id="@+id/naamSmoelenBoek"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:text="Frank Otten"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@+id/profielPicSmoelenBoek"
        app:layout_constraintStart_toEndOf="@+id/profielPicSmoelenBoek"
        app:layout_constraintTop_toBottomOf="@+id/profielPicSmoelenBoek" />

    <TextView
        android:id="@+id/textView17"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="15dp"
        android:text=">"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/naamSmoelenBoek" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是我有一个recyclerview的片段,其中需要用上面的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:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/offer_bg"
        android:adjustViewBounds="true"
        android:scaleType="center"/>

    <Button
        android:id="@+id/searchInSmoelenBoek"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginTop="60dp"
        android:layout_marginEnd="15dp"
        android:background="@color/lightGray"
        android:hint="Zoek in smoelenboek"
        android:padding="10dp"
        android:textAlignment="textStart"
        android:textColor="@color/colorAccent"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginTop="60dp"
        android:text="Vind gemakkelijk alle\naangesloten orthodontisten"
        android:textColor="@color/white"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/searchInSmoelenBoek" />

    <TextView
        android:id="@+id/textView15"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        android:text="Aangesloten orthodontisten"
        android:textAlignment="textStart"
        android:textColor="@color/browser_actions_title_color"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView8" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/smoelenboekRecyclerView"
        android:layout_width="409dp"
        android:layout_height="440dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView15"/>
</androidx.constraintlayout.widget.ConstraintLayout>

我在这里做错了什么或想念什么?我想我仍然需要正确设置适配器,但是我也无法弄清楚...希望任何人都能帮助我!

android android-studio kotlin android-recyclerview android-adapter
1个回答
0
投票
// Set layout manager for recycler view
smoelenboekRecyclerView.layoutManager = LinearLayoutManager(this);
// Set adapter to recycler view
smoelenboekRecyclerView.adapter = SmoelenBoekAdapter(profiles)   
© www.soinside.com 2019 - 2024. All rights reserved.