I have been sitting with this error for so long. I tried almost every solution available on the internet. Most of the solutions were asking to try different versions of these lines. I am still getting the same error.
val recyclerview = view.findViewById<RecyclerView>(R.id.memberRecyclerView)
val layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(activity)
recyclerview.setLayoutManager(layoutManager)
recyclerview.setHasFixedSize(true)
val adapter = MemberListAdapter(data)
recyclerview.setAdapter(adapter)
It would be helpful if someone helps me figure out the issue. Thanks in advance!
AddingGroup.kt
class AddingGroup : Fragment() {
//Declaring
private lateinit var group_name : EditText
private lateinit var member_email : EditText
private lateinit var imageAdd1 : ImageView
private lateinit var btn_submit : Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_adding_group, container, false)
group_name = view.findViewById(R.id.group_name)
member_email = view.findViewById(R.id.member_email)
imageAdd1 = view.findViewById(R.id.imageAdd1)
btn_submit = view.findViewById(R.id.btn_submit)
val recyclerview = view.findViewById<RecyclerView>(R.id.memberRecyclerView)
val layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(activity)
recyclerview.setLayoutManager(layoutManager)
recyclerview.setHasFixedSize(true)
//adding member email to array list
val data = ArrayList<MemberList>()
imageAdd1.setOnClickListener{
data.add(MemberList(member_email.toString(), R.drawable.ic_remove_circle));
}
val adapter = MemberListAdapter(data)
recyclerview.setAdapter(adapter)
//adding group to firebase
btn_submit.setOnClickListener {
//
}
return view
}
}
MemberListAdapter.kt
class MemberListAdapter(private val member_list: ArrayList<MemberList>) : RecyclerView.Adapter<ViewHolder>() {
override fun getItemCount(): Int {
return member_list.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.fragment_group_card, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val MemberList = member_list[position]
// sets the image to the imageview from our itemHolder class
holder.imageView.setImageResource(MemberList.removeBtn)
// sets the text to the textview from our itemHolder class
holder.textView.text = MemberList.memberEmail
}
}
class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
val imageView: ImageView = itemView.findViewById(R.id.removebtn)
val textView: TextView = itemView.findViewById(R.id.usermail)
}
Recyclerview:
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/memberRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintVertical_bias="0.288"
tools:ignore="MissingConstraints"
tools:itemCount="5"
tools:listitem="@layout/fragment_group_card"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
fragment_group_card.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@ id/usermail"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="UserName"
android:textColor="#000000"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="25dp" />
<ImageView
android:id="@ id/removebtn"
android:layout_width="wrap_content"
android:layout_height="25dp"
app:srcCompat="@drawable/ic_remove_circle"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="350dp"
tools:layout_editor_absoluteY="6dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</FrameLayout>
CodePudding user response:
You need to manage empty conditions as per deletion of items
Can you change the fragment_group_card.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
app:cardCornerRadius="10dp"
app:cardElevation="10dp">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@ id/usermail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:hint="UserName"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="#000000" />
<ImageView
android:id="@ id/removebtn"
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_weight="0.2"
app:srcCompat="@drawable/ic_launcher_background" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.cardview.widget.CardView>
</FrameLayout>
Other code looks fine only kotlin cleanup required for the code.
recyclerview.layoutManager = layoutManager
recyclerview.adapter = adapter