I'm trying to Develop an android apps with recycler view. So I created an item view, a model class and adapter. In Adatper.java
class inside viewHolder method, showing errors while I initializing variables, and I can't figure out what is wrong with the following piece of code.
here is code for item_view_model.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="90dp">
<androidx.cardview.widget.CardView
android:id="@ id/imageView"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="20dp"
app:cardCornerRadius="60dp"
app:cardElevation="10dp">
<ImageView
android:id="@ id/imagesme"
android:layout_width="60dp"
android:layout_height="60dp"
android:scaleType="centerCrop"
android:src="@drawable/download" />
</androidx.cardview.widget.CardView>
<TextView
android:id="@ id/textname"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@ id/imageView"
android:layout_marginTop="20dp"
android:text="@string/syed_bipul_rahman"
android:textColor="#000"
android:textStyle="bold" />
<TextView
android:id="@ id/podobi"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="@string/professor"
android:layout_below="@id/textname"
android:layout_toRightOf="@ id/imageView"
android:textColor="#000"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
here is code of ModelClass.java
package com.school.recyclerview;
public class ModelClass {
public ModelClass(int imagesme, String textname, String podobi) {
this.imagesme = imagesme;
this.textname = textname;
this.podobi = podobi;
}
private int imagesme;
private String textname;
private String podobi;
public int getImagesme() {
return imagesme;
}
public void setImagesme(int imagesme) {
this.imagesme = imagesme;
}
public String getTextname() {
return textname;
}
public void setTextname(String textname) {
this.textname = textname;
}
public String getPodobi() {
return podobi;
}
public void setPodobi(String podobi) {
this.podobi = podobi;
}
}
and these codes for ** Adapter.java**
package com.school.recyclerview;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private List<ModelClass> userList;
public Adapter(List<ModelClass> userList) {
this.userList = userList;
}
@NonNull
@Override
public Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view_model, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull Adapter.ViewHolder holder, int position) {
int userImg = userList.get(position).getImagesme();
String userNames = userList.get(position).getTextname();
String userPost = userList.get(position).getPodobi();
holder.setData(userImg, userNames, userPost);
}
@Override
public int getItemCount() {
return userList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private int imageview;
private String textmyname, podobi;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageview = itemView.findViewById(R.id.imagesme);
textmyname = itemView.findViewById(R.id.textname);
podobi = itemView.findViewById(R.id.podobi);
}
public void setData(int userImg, String userNames, String userPost) {
imageview.setImageResource(userImg);
textmyname.setText(userNames);
podobi.setText(userPost);
}
}
}
How to solve this problem??
CodePudding user response:
Try changing the types of imageview
, textmyname
and podobi
in your ViewHolder
Class:
private ImageView imageview;
private TextView textmyname, podobi;
instead of
private int imageview;
private String textmyname, podobi;