6

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?

Igal
  • 5,833
  • 20
  • 74
  • 132

4 Answers4

17

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.

Akbari Dipali
  • 2,173
  • 1
  • 20
  • 26
4

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.

the-ginger-geek
  • 7,041
  • 4
  • 27
  • 45
2

Check you code i think you have imported android.R in your code. Remove it.

SKT
  • 1,821
  • 1
  • 20
  • 32
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22
1

Project -> Clean This regenerates that R file and this should work also.

Noman Arain
  • 1,172
  • 4
  • 19
  • 45