gpt4 book ai didi

java - findViewById() 无法在我的自定义适配器中解析

转载 作者:行者123 更新时间:2023-11-29 20:32:03 24 4
gpt4 key购买 nike

大家好,我刚刚为我的小应用程序创建了自定义行和自定义适配器。手头的问题是当我尝试在自定义适配器中使用 findViewById 方法来获取对自定义布局中 TextView 的引用时。 Android 工作室将字母设为红色并给我一个错误提示无法解析方法 findViewById(int)。这是我的自定义适配器代码:

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;

}
}

最佳答案

只需在 findViewById 之前添加 bookRow

View bookRow=inflator.inflate(R.layout.row_layout, parent, false);
Book book=getItem(position);
TextView bookNameLabel=(TextView)bookRow.findViewById(R.id.bookNameLabel);

关于java - findViewById() 无法在我的自定义适配器中解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31787012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com