无法解决 imageView 中的 setText() 方法。

huangapple go评论148阅读模式
英文:

Cannot resolve method setText() in imageView

问题

这是您提供的内容的翻译:

我正在尝试在Android Studio中使用RecyclerView和CardView完成一个简单的项目,按照这个教程。但是在 onBindViewHolder 函数的第32行,我得到了一个错误,错误提示是不能解析 imageView 中的 setText() 方法,我不知道为什么,因为 mTitle 不是一个 ImageView,它是一个 TextView

package com.example.demoproject;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import android.content.Context;

public class MyAdapter extends RecyclerView.Adapter<MyHolder> {
    Context c;
    ArrayList<Model> models; // 这个数组列表创建了一个数组列表,其中的参数在我的模型类中定义

    public MyAdapter(Context c, ArrayList<Model> models) {
        this.c = c;
        this.models = models;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row, null); // 这一行膨胀了我的行
        return new MyHolder(view); // 这将把我的视图返回给 holder 类
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
        myHolder.mTitle.setText(models.get(i).getTitle()); // 这里的 i 是位置

    }

    @Override
    public int getItemCount() {
        return 0;
    }
}

MyHolder.java:

package com.example.demoproject.

import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MyHolder extends RecyclerView.ViewHolder {
    ImageView mImaeView, mTitle;
    public MyHolder(@NonNull View itemView) {
        super(itemView);
        this.mImaeView = itemView.findViewById(R.id.imageIv);
        this.mTitle = itemView.findViewById(R.id.titleTv);
    }
}
英文:

I'm trying to do a simple project with Recyclerview and Cardview in Android Studio following this tutorial.
But I'm getting an error in onBindViewHolder function line 32 saying it can't resolve the method setText() in imageView and I don't know why since mTitle is not an ImageView and it is a TextView:

package com.example.demoproject;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import android.content.Context;

public class MyAdapter extends RecyclerView.Adapter&lt;MyHolder&gt; {
    Context c;
    ArrayList&lt;Model&gt; models; // this array list creates a list of arrays which parameters define in my model class

    public MyAdapter(Context c, ArrayList&lt;Model&gt; models) {
        this.c = c;
        this.models = models;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row,null); // this line inflate my row
        return new MyHolder(view); // this will return my view to holder class
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
        myHolder.mTitle.setText(models.get(i).getTitle()); // here i is position

    }

    @Override
    public int getItemCount() {
        return 0;
    }
}

MyHolder.java :

package com.example.demoproject.

import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MyHolder extends RecyclerView.ViewHolder {
    ImageView mImaeView, mTitle;
    public MyHolder(@NonNull View itemView) {
        super(itemView);
        this.mImaeView = itemView.findViewById(R.id.imageIv);
        this.mTitle = itemView.findViewById(R.id.titleTv);
    }
}

答案1

得分: 1

你的 mTile 不是一个 TextView,而是一个 ImageView。将其更改为以下内容:

ImageView mImaeView; 
TextView mTitle;

并确保 itemView.findViewById(R.id.titleTv); 返回的是 TextView。检查你的 XML 文件(RecyclerView 项)确保一切设置正确,titleTv 是一个 TextView

英文:

Your mTile is not a TextView it is a ImageView. Change it to this:

ImageView mImaeView; 
TextView mTitle;

And make sure that itemView.findViewById(R.id.titleTv); returns TextView. Check in Your xml file (recycler view item) that You made it eveything well and titleTv is a TextView

答案2

得分: 1

因为setText()方法是在TextView上设置的,而不是在ImageView上。

英文:

because setText() method is set on textview not in imageView

huangapple
  • 本文由 发表于 2020年9月10日 00:39:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63815973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定