Java 无法在自定义适配器中解析findViewById()
大家好,我刚刚为我的小应用程序创建了一个自定义行和自定义适配器。当前的问题是,当我试图在自定义适配器中使用findviewbyd方法来获取对自定义布局中TextView的引用时。Android工作室将字母改为红色,并给我一个错误,表示无法解析方法findViewById(int)。 以下是自定义适配器的代码:Java 无法在自定义适配器中解析findViewById(),java,android,Java,Android,大家好,我刚刚为我的小应用程序创建了一个自定义行和自定义适配器。当前的问题是,当我试图在自定义适配器中使用findviewbyd方法来获取对自定义布局中TextView的引用时。Android工作室将字母改为红色,并给我一个错误,表示无法解析方法findViewById(int)。 以下是自定义适配器的代码: package com.example.opeyemi.storytime; import android.app.Activity; import android.content.Co
package com.example.opeyemi.storytime;
import android.app.Activity;
import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class listViewBookAdapter extends ArrayAdapter<Book> {
public listViewBookAdapter(Context context, ArrayList<Book>books) {
super(context,R.layout.row_layout,books);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflator=LayoutInflater.from(getContext());
View bookRow=inflator.inflate(R.layout.row_layout, parent, false);
Book book=getItem(position);
TextView bookNameLabel=(TextView)findViewById(R.id.bookNameLabel);
return bookRow;
}
}
package com.example.opeyemi.storytime;
导入android.app.Activity;
导入android.content.Context;
导入android.text.Layout;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.TextView;
导入java.util.ArrayList;
公共类listViewBookAdapter扩展了ArrayAdapter{
公共listViewBookAdapter(上下文上下文,ArrayListbooks){
超级(上下文、右布局、行布局、书籍);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气器=LayoutInflater.from(getContext());
查看bookRow=充气机。充气(R.layout.row\u布局,父级,false);
Book Book=getItem(位置);
TextView bookNameLabel=(TextView)findViewById(R.id.bookNameLabel);
归还书架;
}
}
只需在findViewById
View bookRow=inflator.inflate(R.layout.row_layout, parent, false);
Book book=getItem(position);
TextView bookNameLabel=(TextView)bookRow.findViewById(R.id.bookNameLabel);
只需将
bookRow
添加到findViewById
View bookRow=inflator.inflate(R.layout.row_layout, parent, false);
Book book=getItem(position);
TextView bookNameLabel=(TextView)bookRow.findViewById(R.id.bookNameLabel);
因为您正在膨胀布局,它返回布局视图,并将其作为参考变量指定给bookRow
因为您正在膨胀布局,该布局返回布局视图,并将其作为参考变量分配给bookRow。
ArrayAdapter
没有方法findViewById
…但是视图
有它。。。询问了数百万次TextView booknamelab=(TextView)bookRow.findViewById(R.id.booknamelab)代码>。还可以查看视图保持器模式ArrayAdapter
没有方法findViewById
…但是视图有方法。。。询问了数百万次TextView booknamelab=(TextView)bookRow.findViewById(R.id.booknamelab)代码>。还可以查看视图保持架模式