不存在类型变量的实例,因此 Integer 符合 View
no instance(s) of type variable(s) exist so that Integer conforms to View
我正在尝试开发一个 android 具有回收站视图的应用程序。所以我创建了一个项目视图、一个模型 class 和适配器。 在 Adatper.java
class inside viewHolder 方法中,初始化变量时显示错误,我无法弄清楚以下代码有什么问题。
这是 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>
这里是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;
}
}
这些代码用于 ** 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);
}
}
}
如何解决这个问题??
尝试更改 imageview
、textmyname
和 podobi
的类型 ViewHolder
Class:
private ImageView imageview;
private TextView textmyname, podobi;
而不是
private int imageview;
private String textmyname, podobi;
我正在尝试开发一个 android 具有回收站视图的应用程序。所以我创建了一个项目视图、一个模型 class 和适配器。 在 Adatper.java
class inside viewHolder 方法中,初始化变量时显示错误,我无法弄清楚以下代码有什么问题。
这是 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>
这里是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;
}
}
这些代码用于 ** 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);
}
}
}
如何解决这个问题??
尝试更改 imageview
、textmyname
和 podobi
的类型 ViewHolder
Class:
private ImageView imageview;
private TextView textmyname, podobi;
而不是
private int imageview;
private String textmyname, podobi;