Recycler View linear layout manager returning null - java

I have a recycler view which contains multiple items and each item in the recycler view contains a horizontal recycler view.The problem I am encountering is that the layout manager is null.
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
This is my code so far.I have checked that the data I am receiving is intact.
RecipeAdapter ( The main adapter )
public class RecipeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<Object> items;
private final int RECIPE = 0, JOKE = 1;
public RecipeAdapter(Context context) {
this.context = context;
this.items = new ArrayList<>();
}
public void setItems(List<Object> items) {
this.items = items;
}
public void add(Object object) {
items.add(object);
notifyItemInserted(items.size());
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder;
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case RECIPE:
View recipe = inflater.inflate(R.layout.item_recipe, parent, false);
viewHolder = new ViewHolder_Recipe(recipe);
break;
case JOKE:
View joke = inflater.inflate(R.layout.item_joke, parent, false);
viewHolder = new ViewHolder_Joke(joke);
break;
default:
View recipe_default = inflater.inflate(R.layout.item_recipe, parent, false);
viewHolder = new ViewHolder_Recipe(recipe_default);
break;
}
return viewHolder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case RECIPE:
ViewHolder_Recipe viewHolderRecipe = (ViewHolder_Recipe) holder;
configureRecipeHolder(viewHolderRecipe, position);
break;
case JOKE:
ViewHolder_Joke viewHolderJoke = (ViewHolder_Joke) holder;
configureJokeHolder(viewHolderJoke, position);
break;
default:
ViewHolder_Recipe viewHolder_recipe_default = (ViewHolder_Recipe) holder;
configureRecipeHolder(viewHolder_recipe_default, position);
break;
}
}
private void configureJokeHolder(ViewHolder_Joke viewHolderJoke, int position) {
}
private void configureRecipeHolder(ViewHolder_Recipe viewHolderRecipe, int position) {
RecipeDetailed recipe = (RecipeDetailed) items.get(position);
Glide.with(context)
.load(recipe.getImage())
.into(viewHolderRecipe.getRecipe_image());
viewHolderRecipe.getRecipe_name().setText(recipe.getTitle());
viewHolderRecipe.getRecipe_prep().setText(recipe.getReadyInMinutes());
viewHolderRecipe.getRecipe_serves().setText(recipe.getServings());
viewHolderRecipe.getIngredientAdapter().setIngredients(recipe.getExtendedIngredients());
}
#Override
public int getItemViewType(int position) {
if (items.get(position) instanceof RecipeDetailed) {
return RECIPE;
} else if (items.get(position) instanceof Joke) {
return JOKE;
}
return super.getItemViewType(position);
}
#Override
public int getItemCount() {
return items.size();
}
}
The ViewHolder for that Adapter -- ViewHolder_Recipe
public class ViewHolder_Recipe extends RecyclerView.ViewHolder {
private CircularImageView recipe_image;
private TextView recipe_name;
private TextView recipe_prep;
private TextView recipe_serves;
private RecyclerView recyclerView;
private RecipeIngredientAdapter ingredientAdapter;
public ViewHolder_Recipe(View itemView) {
super(itemView);
recipe_image = (CircularImageView) itemView.findViewById(R.id.recipe_image);
recipe_name = (TextView) itemView.findViewById(R.id.recipe_name);
recipe_prep = (TextView) itemView.findViewById(R.id.recipe_prep);
recipe_serves = (TextView) itemView.findViewById(R.id.recipe_serves);
recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView);
ingredientAdapter = new RecipeIngredientAdapter(itemView.getContext());
recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()
, LinearLayoutManager.HORIZONTAL, false));
recyclerView.setAdapter(ingredientAdapter);
}
public RecipeIngredientAdapter getIngredientAdapter() {
return ingredientAdapter;
}
public CircularImageView getRecipe_image() {
return recipe_image;
}
public TextView getRecipe_name() {
return recipe_name;
}
public TextView getRecipe_prep() {
return recipe_prep;
}
public TextView getRecipe_serves() {
return recipe_serves;
}
public RecyclerView getRecyclerView() {
return recyclerView;
}
}
The child adapter -- RecipeIngredientAdapter
public class RecipeIngredientAdapter extends RecyclerView.Adapter<ViewHolderRecipeIngredient> {
private Context context;
private ArrayList<Ingredient> ingredients;
public RecipeIngredientAdapter(Context context) {
this.context = context;
}
public void setIngredients(ArrayList<Ingredient> ingredients) {
this.ingredients = ingredients;
}
#Override
public ViewHolderRecipeIngredient onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item_recipe_ingredients, parent, false);
return new ViewHolderRecipeIngredient(view);
}
#Override
public void onBindViewHolder(ViewHolderRecipeIngredient holder, int position) {
final Ingredient ingredient = ingredients.get(position);
Glide.with(context)
.load(ingredient.getImage())
.into(holder.getIngredient_image());
}
#Override
public int getItemCount() {
return 0;
}
}
The viewholder for that is a simple viewholder which contains an image.
I have seen posts online which seem to do the exact same thing and get it working,what exactly am i missing here?

From the error, it sounds like the RecyclerView is null, not the LayoutManager. Make sure you are using the proper id from the layout. are you sure the proper layout id of the RecyclerView is R.id.recyclerView?

Related

How can i modify getItemViewType() with adapter input as a string and cursor?

I have created a recycler view and want to put two lists in it.
The first list is just a string that gets changed into fonts, which I have implemented in the onBindView method.
The second list is the cursor which includes the favorite fonts of the users.
I followed this answer on SO but I don't have any idea how I'm going to modify the getItemViewType() method so that I can get to know which list type is currently calling for view.
My adapter code
here data is the class that contains the list of fonts
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int VIEW_TYPE_TEXTVIEW = 0;
private final int VIEW_TYPE_ITEM_1 = 1;
private int total_no_of_row;
private Context context;
private String str;
private Cursor cursor;
private final LayoutInflater inflater;
public MyAdapter(Context context, int total_no_of_row, String str , Cursor cursor) {
this.context = context;
this.total_no_of_row = total_no_of_row;
this.str = str;
this.cursor = cursor;
inflater = LayoutInflater.from(context);
}
#Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if(viewType == VIEW_TYPE_TEXTVIEW) {
View view = inflater.inflate(R.layout.row_layout, parent, false);
return new Item1Holder(view);
}
else if (viewType == VIEW_TYPE_ITEM_1){
View view = inflater.inflate(R.layout.row_layout, parent, false);
return new Item2Holder(view);
}
return null;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
if(holder instanceof Item1Holder){
data d = new data();
cursor.moveToPosition(position);
Log.d("this2",cursor.getInt(1)+"");
String temp = d.Convert(str, cursor.getInt(1));
((Item1Holder)holder).textView.setText(temp);
((Item1Holder)holder).rowNumber.setText("⚫");
((Item1Holder)holder).fav.setImageResource(R.drawable.ic_baseline_favorite_24);
}else if(holder instanceof Item2Holder){
data d = new data();
String temp = d.Convert(str, position);
((Item2Holder)holder).textView.setText(temp);
((Item2Holder)holder).rowNumber.setText((position + 1) + ".");
if (util.COPY) {
if (position == util.CURRENT_POSITION) {
((Item2Holder)holder).imageView.setBackgroundResource(R.drawable.background_when_row_clicked);
} else {
((Item2Holder)holder).imageView.setBackgroundResource(R.drawable.background_when_row_clicked_two);
}
}
}
}
#Override
public int getItemCount() {
return total_no_of_row + cursor.getCount();
}
class Item1Holder extends RecyclerView.ViewHolder {
TextView textView;
ImageView imageView , fav;
TextView rowNumber;
public Item1Holder(#NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text_body);
imageView = itemView.findViewById(R.id.copy_png);
rowNumber = itemView.findViewById(R.id.row_number);
fav = itemView.findViewById(R.id.fav);
}
}
class Item2Holder extends RecyclerView.ViewHolder {
TextView textView;
ImageView imageView , fav;
TextView rowNumber;
public Item2Holder(#NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text_body);
imageView = itemView.findViewById(R.id.copy_png);
rowNumber = itemView.findViewById(R.id.row_number);
fav = itemView.findViewById(R.id.fav);
}
}
}
in main activity
here temp is the string that users want to convert into the fonts
Cursor cursor = show();
myAdapter = new MyAdapter(this , 500 , temp , cursor);
recyclerView.setAdapter(myAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
I'm not a professional android developer I'm still learning the stuff. so sorry if my code is too bad to understand
You are passing null in the onCreateViewHolder() method, and not specified position in getItemViewType() method. All you have to do is, update your code. do not pass the null value in the onCreateViewHolder method. Below code, I've tried, and works perfectly. Happy coding...
private static final int CATEGORY = 1;
private static final int NAME = 2;
private Collection<CategorizedName> data;
#Override
public int getItemViewType(int position) {
if (items.get(position) instanceof Category) {
return CATEGORY;
} else if (items.get(position) instanceof Name) {
return NAME;
}
throw new RuntimeException('error');
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
switch (viewType) {
case CATEGORY:
View categoryView = inflater.inflate(R.layout.viewholder_category, viewGroup,
false);
viewHolder = new CategoryViewHolder(categoryView);
break;
case NAME:
View nameView = inflater.inflate(R.layout.viewholder_name, viewGroup, false);
viewHolder = new NameViewHolder(nameView);
break;
}
return viewHolder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
switch (viewHolder.getItemViewType()) {
case USER:
CategoryViewHolder categoryViewHolder = (CategoryViewHolder) viewHolder;
bindCategoryViewHolder(categoryViewHolder, position);
break;
case IMAGE:
NameViewHolder nameViewHolder = (NameViewHolder) viewHolder;
bindNameViewHolder(nameViewHolder, position);
break;
}
}
Replace your getItemViewType() with this
#Override
public int getItemViewType(int position){
if(position < total_no_of_row){
return VIEW_TYPE_TEXTVIEW;
}
if(position - total_no_of_row < cursor.getCount()){
return VIEW_TYPE_ITEM_1;
}
return -1;
}
you can also refer to my answer this

Add static default item in RecyclerView

I have a horizontal RecyclerView that displays 7 images from the users device (changing as user keeps taking pictures). I would like to add a default icon at the end of the 7 images, with an onclick capability. How can I go about doing this?
Something like this:
In the image above the plus icon should be the default icon, however it should be at the end of the images.
UPDATED:
RecentPhotosAdapter.java
public class RecentPhotosAdapter extends RecyclerView.Adapter<RecentPhotosAdapter.ViewHolder> {
// Variables
private Context mContext;
private ArrayList<String> mImage;
private int VIEW_TYPE_DEFAULT = 0;
private int VIEW_TYPE_IMAGE = 1;
// Limit the recent photo selection
int RECENT_PHOTO_LIMIT = 7;
public RecentPhotosAdapter(Context mContext, ArrayList<String> mImage) {
this.mContext = mContext;
this.mImage = mImage;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_IMAGE) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recent_photos_item, parent, false);
return new ViewHolder(view);
}
else {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.default_select_photos_item, parent, false);
return new ViewHolder(view);
}
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
if(position < Math.min(mImage.size(), RECENT_PHOTO_LIMIT)){
String images = mImage.get(position);
// Recent Photos
Glide.with(mContext)
.load(images)
.placeholder(R.drawable.background_gallery_placeholder)
.transform(new CenterCrop(), new RoundedCorners(30))
.into(holder.recent_photos_iv);
// Default select photos
holder.recent_photos_iv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something here
// Take user to image select slider
}
});
}
else {
//code for default view
}
}
#Override
public int getItemViewType(int position) {
return (position == mImage.size()) ? VIEW_TYPE_DEFAULT : VIEW_TYPE_IMAGE;
}
#Override
public int getItemCount() {
return Math.min(mImage.size(), RECENT_PHOTO_LIMIT) + 1;
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView recent_photos_iv;
ImageView default_select_photo_iv;
public ViewHolder(View itemView) {
super(itemView);
recent_photos_iv = itemView.findViewById(R.id.recent_photos_iv);
default_select_photo_iv = itemView.findViewById(R.id.default_select_photo_iv);
}
}
}
You can achieve this by making the default icon as a "ViewType" of your adapter.
Override getItemViewType to return a different value for your last item.
#Override
public int getItemViewType(int position) {
return (position == mData.size()) ? VIEW_TYPE_DEFAULT : VIEW_TYPE_IMAGE;
}
Now in the onCreateViewHolder, handle both the view Types.
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_IMAGE) {
//Create viewholder for your images
}
else {
//Create viewholder for default view
}
}
Add 1 to your ItemList size to accomodate this extra view:
#Override
public int getItemCount() {
return mData.size() + 1;
}
Your Updated code:
public class RecentPhotosAdapter extends RecyclerView.Adapter<RecentPhotosAdapter.ViewHolder> {
// Variables
private Context mContext;
private ArrayList<String> mImage;
private int VIEW_TYPE_DEFAULT=0;
private int VIEW_TYPE_IMAGE=1;
public RecentPhotosAdapter(Context mContext, ArrayList<String> mImage) {
this.mContext = mContext;
this.mImage = mImage;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_IMAGE) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recent_photos_item, parent, false);
return new ViewHolder(view);
}
else {
//Create viewholder for default view
}
}
#Override
public int getItemViewType(int position) {
return (position == Math.min(mImage.size(), RECENT_PHOTO_LIMIT)) ?VIEW_TYPE_DEFAULT : VIEW_TYPE_IMAGE;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
if(position < Math.min(mImage.size(), RECENT_PHOTO_LIMIT)){
String images = mImage.get(position);
Glide.with(mContext)
.load(images)
.placeholder(R.drawable.background_gallery_placeholder)
.transform(new CenterCrop(), new RoundedCorners(30))
.into(holder.recent_photos_iv);
holder.recent_photos_iv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something here
// Take user to image select slider
}
});
}
else {
//code for default view
}
}
#Override
public int getItemCount() {
// Limit the recent photo selection
int RECENT_PHOTO_LIMIT = 7;
return Math.min(mImage.size(), RECENT_PHOTO_LIMIT)+1;
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView recent_photos_iv;
public ViewHolder(View itemView) {
super(itemView);
recent_photos_iv = itemView.findViewById(R.id.recent_photos_iv);
}
}
}

How to store items from inside onBindViewHolder to array

I have a number of buttons inside recycview
I just wanted to add them in array and then i get them to add to them some jobs.
At first i defined array name "buttons"
ToggleButton buttons[] = new ToggleButton[5];
Then i put values in it
buttons[position]=holder.playBtn;
Now I want to press one of them
change all the backgrounds of all buttons
And so i used this function :
closeAll()
,but I didn't succeeding
// My class
public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder> {
public RecyclerViewClickListener mListener;
private MyListData[] listdata;
private Context context;
private ToggleButton [] listBtnPlyStop = null;
ToggleButton buttons[] = new ToggleButton[5];
public MyListAdapter(Context context ,MyListData[] listdata , RecyclerViewClickListener mListener) {
this.mListener =mListener;
this.listdata = listdata;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem= layoutInflater.inflate(R.layout.row_list_azcar, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final MyListData myListData = listdata[position];
holder.textView.setText(listdata[position].getDescription());
buttons[position]=holder.playBtn;
holder.playBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
mListener.onClick(compoundButton,position);
setBgBtn(compoundButton,checked,position);
}
});
}
private void setBgBtn(CompoundButton compoundButton , boolean checked,int id) {
if(checked){
compoundButton.setBackground(ContextCompat.getDrawable(context, R.drawable.btnpause));
}else{
compoundButton.setBackground(ContextCompat.getDrawable(context, R.drawable.btnplay));
}
closeAll();
}
private void closeAll(){
for(int j=0; j<buttons.length-1;j++) {
buttons[j].setBackgroundResource(R.drawable.btnpause);
}
}
#Override
public int getItemCount() {
return listdata.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView textView;
public LinearLayout linearLayout;
public ToggleButton toggleButton,playBtn;
public RadioButton radioButton1,radioButton2,radioButton3;
private RecyclerViewClickListener mListener;
public ViewHolder(View itemView){
super(itemView);
//this.itemView = itemView;
this.toggleButton =(ToggleButton) itemView.findViewById(R.id.bt_check2);
this.playBtn =(ToggleButton) itemView.findViewById(R.id.bt_play);
this.radioButton1 = (RadioButton)itemView.findViewById(R.id.radio_btn1);
this.radioButton2= (RadioButton)itemView.findViewById(R.id.radio_btn2);
this.radioButton3= (RadioButton)itemView.findViewById(R.id.radio_btn3);
this.textView = (TextView) itemView.findViewById(R.id._txt_kind_of_azkar);
linearLayout = (LinearLayout)itemView.findViewById(R.id.l_containe_row);
}
}
}
I would suggest creating a dynamic array list of ToggleButtons, like so:
private ArrayList<ToggleButton> buttons;
public MyListAdapter(Context context ,MyListData[] listdata , RecyclerViewClickListener mListener) {
this.mListener =mListener;
this.listdata = listdata;
this.context = context;
this.buttons = new ArrayList<>();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem= layoutInflater.inflate(R.layout.row_list_azcar, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final MyListData myListData = listdata[position];
holder.textView.setText(listdata[position].getDescription());
buttons.add(holder.playBtn);
...
private void closeAll(){
for(int j=0; j<buttons.size(); j++) {
buttons.get(j).setBackgroundResource(R.drawable.btnpause);
}
}

Android. Java. RecyclerAdapter

I have two different card view in one layout. I need that The latest news is added to one card view, and the rest in another. For exapmle, on image
MyRecyclerAdapter code
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.CustomViewHolder> {
private List<FeedItem> feedItemList;
private Context mContext;
public MyRecyclerAdapter(Context context, List<FeedItem> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
FeedItem feedItem = feedItemList.get(i);
//Download image using picasso library
Picasso.with(mContext).load(feedItem.getThumbnail())
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(customViewHolder.imageView);
//Setting text view title
customViewHolder.textView.setText(Html.fromHtml(feedItem.getTitle()));
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
CustomViewHolder holder = (CustomViewHolder) view.getTag();
int position = holder.getPosition();
FeedItem feedItem = feedItemList.get(position);
Toast.makeText(mContext, feedItem.getTitle(), Toast.LENGTH_SHORT).show();
}
};
//Handle click event on both title and image click
customViewHolder.textView.setOnClickListener(clickListener);
customViewHolder.imageView.setOnClickListener(clickListener);
customViewHolder.textView.setTag(customViewHolder);
customViewHolder.imageView.setTag(customViewHolder);
}
#Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
protected ImageView imageView;
protected TextView textView;
public CustomViewHolder(View view) {
super(view);
this.imageView = (ImageView) view.findViewById(R.id.thumbnail);
this.textView = (TextView) view.findViewById(R.id.title);
}
}
}
The approach I would use if I were in you, to accomplish what you posted in the picture is to override getItemViewType and have two different types of ViewHolder, once for the header, the one with the big image and the other adapter for the small rows. onBindViewHolder gets the type as parameter. In your case is int i.
I write a quickdemo like this:
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList mData = new ArrayList();
private LayoutInflater mInflater;
private TreeSet mSeparatorSet = new TreeSet();
public MyCustomAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparator(String item) {
mData.add(item);
mSeparatorSet.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return mSeparatorSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public Object getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + "type " + type);
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.item1, null);
holder.textView = (TextView) convertView.findViewById(R.id.text_view);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.item2, null);
holder.textView = (TextView) convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mData.get(position) + "");
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
or if you want to use header and footer, create headerView layout:
View header = (View) getLayoutInflater().inflate(R.layout.headerView,null);
listView.addHeaderView(header);

Android: navigation drawer with custom adapter onItemClickListener

I've been trying set custom ListView adapter for navigation drawer it derived from BaseAdapter so when I set OnItemClickListener it doesn't work on either ListView.OnItemClickListener and AdapterView.OnItemClickListener.
Googled around but all examples derive from ArrayAdapter since BaseAdapter desired.
Can anyone help me on this?
NavigationDrawer fragment onCreateView
drawerItems = new ArrayList<DrawerItem>();
drawerItems.add(DrawerItem.create(1, getString(R.string.ideas), getDrawable(R.drawable.ic_action_diamond)));
drawerItems.add(DrawerItem.create(2, getString(R.string.categories), getDrawable(R.drawable.ic_action_categories)));
drawerItems.add(DrawerItem.create(3, getString(R.string.priorities), getDrawable(R.drawable.ic_action_priorities)));
mDrawerListAdapter = MenuAdapter.newInstance(getActivity(), drawerItems);
mDrawerListView.setItemsCanFocus(true);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, String.valueOf(position));
selectItem(position);
}
});
mDrawerListView.setAdapter(mDrawerListAdapter);
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
My custom adapter
public class MenuAdapter extends BaseAdapter {
private static final String TAG = "AD:MENU";
private final LayoutInflater mLayoutInflater;
private List<DrawerItem> items;
private Context context;
public MenuAdapter(Context ctx, List<DrawerItem> items) {
this.items = items;
this.context = ctx;
this.mLayoutInflater = LayoutInflater.from(ctx);
}
public static MenuAdapter newInstance(Context ctx, List<DrawerItem> items) {
return new MenuAdapter(ctx, items);
}
#Override
public int getCount() {
return items == null ? 0 : items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View v, ViewGroup parent) {
DrawerItem item = (DrawerItem) getItem(position);
ViewHolder holder;
if (v == null) {
v = mLayoutInflater.inflate(R.layout.drawable_item, parent, false);
holder = new ViewHolder();
if (v != null) {
holder.title = (TextView) v.findViewById(R.id.menu_title);
holder.title.setTag(item.id);
v.setTag(holder);
}
} else {
holder = (ViewHolder) v.getTag();
}
holder.title.setText(item.title);
holder.title.setCompoundDrawablesWithIntrinsicBounds(item.icon, null, null, null);
Log.d(TAG, item.title);
return v;
}
public class ViewHolder {
TextView title;
}
}

Categories