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

4

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?

Guberniya answered 15/3, 2013 at 12:14 Comment(4)
Check if you have the correct (or any at all) R.java importedTodd
add here line where you are calling adapter in Activity class.Watchtower
@StefandeBruijn Yes, apparently I had the wrong R class imported. Have no idea how it happened... Thank you! As you were first to answer my question, can you please post it as an answer so I can accept it? Again, thank you very much!Guberniya
Feel free to just accept any other's correct answer :) Good luckTodd
B
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.

Bathhouse answered 15/3, 2013 at 12:23 Comment(1)
Thank you! Yes, that was the problem.Guberniya
U
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.

Undergrowth answered 15/3, 2013 at 12:18 Comment(0)
I
2

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

Insecure answered 15/3, 2013 at 12:19 Comment(0)
O
1

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

Opus answered 17/10, 2013 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.