英文:
RecycleView not shownig any items
问题
I see that you have provided code and descriptions for an Android app using Kotlin and a RecyclerView with CardViews. If you have any specific questions or need assistance with any part of this code, please let me know, and I'll be happy to help.
英文:
I'm new to android and kotlin and I'm having trouble making a recycleView for my app
There are 0 errors in my code, but items don't show up in the simulater and the recyclervie is just blank.
I havent connected it to any database and I was just trying to see how my cardviews would look with some placeholders.
I've implemented recycleView and cardView in my gradle file and here's the rest of my code:
each item: item_project.xml
:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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:id="@+id/crdProjectItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
app:cardCornerRadius="10dp"
app:cardBackgroundColor="@color/grey1">
<RelativeLayout
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp">
<TextView
android:id="@+id/tvProjectTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/btnShare"
android:fontFamily="@font/montserrat_semibold"
android:singleLine="true"
android:text="Project Title Here"
android:textSize="20sp" />
<TextView
android:id="@+id/tvProjectDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@+id/tvProjectTitle"
android:layout_alignBottom="@+id/imgSyncStatus"
android:layout_marginStart="5dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="0dp"
android:layout_toStartOf="@+id/btnShare"
android:layout_toEndOf="@+id/imgSyncStatus"
android:fontFamily="@font/montserrat"
android:singleLine="true"
android:text="November 3, 2023"
android:textSize="15sp" />
<ImageView
android:id="@+id/imgSyncStatus"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_below="@+id/tvProjectTitle"
android:contentDescription="Uploaded"
android:layout_alignParentStart="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:src="@drawable/baseline_sync_24" />
<ImageButton
android:id="@+id/btnShare"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginEnd="0dp"
android:layout_toStartOf="@+id/btnOptions"
android:background="#00000000"
android:src="@drawable/baseline_share_24"
android:contentDescription="Share"
/>
<ImageButton
android:id="@+id/btnOptions"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="0dp"
android:background="#00000000"
android:src="@drawable/baseline_more_vert_24"
android:contentDescription="Options"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
data class: project.kt
:
package com.example.roadsafetycheck
data class Project (
val title: String,
val date: String,
var isSynced: Boolean
)
my adapter: ProjectAdapter.kt
:
package com.example.roadsafetycheck
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class ProjectAdapter (
private val projects : ArrayList<Project>
): RecyclerView.Adapter<ProjectAdapter.ProjectViewHolder>() {
inner class ProjectViewHolder (itemView: View) : RecyclerView.ViewHolder (itemView) {
val pTitle : TextView = itemView.findViewById(R.id.tvProjectTitle)
val pDate : TextView = itemView.findViewById(R.id.tvProjectDate)
val isSync : ImageView = itemView.findViewById(R.id.imgSyncStatus)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProjectViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_project, parent, false)
return ProjectViewHolder(view)
}
override fun onBindViewHolder(holder: ProjectViewHolder, position: Int) {
holder.pTitle.text = projects[position].title
holder.pDate.text = projects[position].date
if (projects[position].isSynced){
holder.isSync.setImageResource(R.drawable.baseline_sync_24)
} else {
holder.isSync.setImageResource(R.drawable.baseline_sync_problem_24)
}
}
override fun getItemCount(): Int {
return projects.size
}
}
main layout: activity_dashboard.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="@+id/dashHeader"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@color/blueL" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="0dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="0dp"
android:fontFamily="@font/montserrat_semibold"
android:text="Welcome"
android:textColor="@color/white"
android:textSize="40sp"
android:textAlignment="center"/>
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvProjects"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/dashHeader"
android:layout_alignParentBottom="true"
android:visibility="visible"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp" />
</RelativeLayout>
and finally the main activity: DashboardActivity.kt
package com.example.roadsafetycheck
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.roadsafetycheck.databinding.ActivityDashboardBinding
class DashboardActivity : AppCompatActivity() {
private lateinit var binding: ActivityDashboardBinding
private lateinit var projectsAdapter : ProjectAdapter
private lateinit var projectsList :ArrayList<Project>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityDashboardBinding.inflate(layoutInflater)
setContentView(binding.root)
projectsList = arrayListOf(
//TODO: connect projectList to database
//temporary data for demo:
Project("Kerman-Yazd Road", "1401/10/13", true),
Project("Kashan-Qom Freeway", "1401/10/30", false),
Project("Tehran-Karaj Highway", "1401/11/20", true),
Project("Tehran-Qom Freeway", "1401/12/2", true),
Project("Resalat Highway", "1401/12/24", true),
Project("Kerman-BandarAbbas Road", "1402/2/15", true)
)
binding.rvProjects.layoutManager = LinearLayoutManager(this)
projectsAdapter = ProjectAdapter(projectsList)
}
}
I watched multiple tutorials and tried what I've found online so far but nothing seems to work
答案1
得分: 0
似乎您没有将适配器设置到您的RecyclerView
中。在实例化适配器后,请尝试以下操作:
binding.rvProjects.adapter = projectsAdapter
英文:
It seems you're not setting your adapter to your RecyclerView
. Try this after instantiating your adapter:
binding.rvProjects.adapter = projectsAdapter
答案2
得分: 0
尝试将此代码添加到您的仪表板活动底部:
binding.rvProjects.apply {
layoutManager = LinearLayoutManager(this@YourActivityName)
adapter = projectsAdapter
}
projectsAdapter = ProjectAdapter(projectsList)
如果您不理解或想查看示例,请查看此项目。
英文:
try to use this code in the bottom of your dashboard activity:
binding.rvProjects.apply{
layoutManager = LinearLayoutManager(this)
adapter = projectsAdapter
}
projectsAdapter = ProjectAdapter(projectsList)
if you dont understand or want to see the example check out this project
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论