Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access R.layout.mylayoutname in my custom ArrayAdapter

I'm trying to create a custom adapter for my Categories list. For some reason I can't access my category_item layout when I'm trying to inflate it. Here's 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 it says resource I should be able to access to my category_item.xml layout - R.layout.category_item. But for some reason I don't see it in the list, I can't access it. In the matter of fact I don't see any of my layouts in the list.

Why is it happening and how can it be fixed?

like image 737
Igal Avatar asked Mar 15 '13 12:03

Igal


2 Answers

Import your packagename.R file and it will work. It may be possiblity that you imported android.R. As you resource exists in you application package, just correct it and it will work.

android.R refers to sdk resources.

like image 173
Akbari Dipali Avatar answered Oct 10 '22 00:10

Akbari Dipali


It is possible that you imported android resources package android.R

Make sure you imported your package resources com.yourpackagename.R. This should fix your problem.

like image 35
the-ginger-geek Avatar answered Oct 10 '22 02:10

the-ginger-geek