英文:
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<MyHolder> {
Context c;
ArrayList<Model> models; // this array list creates a list of arrays which parameters define in my model class
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); // 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论