当前位置: 首页 > 知识库问答 >
问题:

不存在类型变量的实例,因此Integer符合视图

姬振
2023-03-14

我正在尝试开发一款android应用程序,具有回收视图。所以我创建了一个项目视图、一个模型类和适配器。在Adatper中。java在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;
    }
}

下面是**适配器的代码。爪哇**

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);

        }
    }
}


如何解决这个问题??

共有1个答案

齐泰
2023-03-14

尝试更改ViewHolder类中的imageviewtextmynamepodobi的类型:

private ImageView imageview;
private TextView textmyname, podobi;

而不是

private int imageview;
private String textmyname, podobi;
 类似资料: