Unable to access R.layout.mylayoutname in my custom ArrayAdapter

I am trying to create my own adapter for a list of categories. For some reason, I cannot access the category_item layout when I try to inflate it. Here is my code:

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; } 

Where he says resource I should have access to my layout category_item.xml - R.layout.category_item. But for some reason I don’t see it on the list, I can’t access it. In fact, I do not see any of my layouts in the list.

Why is this happening and how can it be fixed?

+4
source share
4 answers

Import the packagename.R file and it will work. You may have imported android.R . Since the resource exists in your application package, just fix it and it will work.

android.R refers to sdk resources.

+13
source

It is possible that you have imported the android.R Android resource package

Be sure to import your com.yourpackagename.R package com.yourpackagename.R . This should solve your problem.

+3
source

Check the code, I think you imported android.R into your code. Remove it.

+2
source

Project -> Clean This restores this R file, and this should also work.

+1
source

Source: https://habr.com/ru/post/1469498/


All Articles