android - 无法在我的自定义 ArrayAdapter 中访问 R.layout.mylayoutname

标签 android android-layout android-arrayadapter layout-inflater custom-adapter

我正在尝试为我的类别列表创建自定义适配器。出于某种原因,我在尝试充气时无法访问我的 category_item 布局。这是我的代码:

public class CategoryAdapter extends ArrayAdapter<Category> {

    LayoutInflater mInflater;
    List<Category> list;

public CategoryAdapter(Context context, int resource, int textViewResourceId, List<Category> objects) {
    super(context, resource, textViewResourceId, objects);

    mInflater = LayoutInflater.from(context);
    list = objects;
}

public View getView(int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;

    if(convertView==null){

        convertView=mInflater.inflate(***resource***, null);
        holder = new ViewHolder();

    }

    return convertView;
}

// Declaring new class ViewHolder
class ViewHolder{
    TextView category_name;
}

它说 resource 我应该能够访问我的 category_item.xml 布局 - R.layout.category_item。但出于某种原因,我没有在列表中看到它,我无法访问它。事实上,我在列表中没有看到任何布局。

为什么会发生这种情况,如何解决?

最佳答案

导入您的packagename.R 文件,它将起作用。您可能导入了 android.R。由于您的资源存在于您的应用程序包中,只需对其进行更正即可。

android.R是指sdk资源。

关于android - 无法在我的自定义 ArrayAdapter 中访问 R.layout.mylayoutname,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15432201/

相关文章:

java - 无法尝试实现从查询中获取随机结果到 Firestore

java - ListView 中的字符串样式,Android Dev

android - 在谷歌驱动器android中的文件夹内创建一个文件夹

java - 通过 MediaRecorder 录制音频

android - 为什么我的 FloatingActionButton 图标是黑色的?

java - 形成和执行 if 语句的问题

java - 为什么我无法在我的适配器中接收数据

javascript - Cordova 3.4.0, Media.setVolume() 如何实现?

Android:为什么我无法为 TextView 背景使用选择器

android - 在 RelativeLayout 中水平居中 RecyclerView