Reputation: 1031
In getView
method when the view type is vTypeItem
it should return all the item from my array list but it returns all except the first one,and because of this the ListView
displays all item except the first one
public class DrawerAdapter extends ArrayAdapter<DrawerCustomList> {
private static final int vTypeHeader = 0;
private static final int vTypeItem = 1;
ArrayList<DrawerCustomList> mList = new ArrayList<>();
public DrawerAdapter(Context context, ArrayList<DrawerCustomList> list) {
super(context,0,list);
mList = list;
}
@Override
public int getItemViewType(int position) {
return (position==0)?vTypeHeader:vTypeItem;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return mList.size();
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
View v = convertView;
MyViewHolder mViewHolder = null;
if (v==null){
if(type==vTypeItem){
v = LayoutInflater.from(getContext()).inflate(R.layout.drawer_single_item,parent,false);
mViewHolder= new MyViewHolder(v,type);
DrawerCustomList val = mList.get(position);
mViewHolder.itemImage.setImageResource(val.getmListImage());
mViewHolder.itemName.setText(val.getmListName());
}else {
v = LayoutInflater.from(getContext()).inflate(R.layout.drawer_header_layout,parent,false);
mViewHolder= new MyViewHolder(v,type);
}
}
v.setTag(mViewHolder);
return v;
}
public static class MyViewHolder{
TextView itemName,headerName,headerEmail;
CircleImageView headerPicture;
ImageView itemImage;
public MyViewHolder(View v,int type){
if(type==vTypeHeader){
headerName = (TextView)v.findViewById(R.id.drawerHeaderName);
headerEmail = (TextView)v.findViewById(R.id.drawerHeaderEmail);
headerPicture = (CircleImageView)v.findViewById(R.id.drawerCircleImageView);
}else {
itemName = (TextView) v.findViewById(R.id.drawerItemName);
itemImage = (ImageView)v.findViewById(R.id.drawerItemImage);
}
}
}
}
Upvotes: 0
Views: 439
Reputation: 111
I changed your code as below, seems to be working fine. Here I have added drawer_single_item layout's drawerItemName,drawerItemImage control in drawer_header_layout. Do the following changes in getView method.
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
View v = convertView;
MyViewHolder mViewHolder = null;
if(type==vTypeItem){
v = LayoutInflater.from(getContext()).inflate(R.layout.drawer_single_item,parent,false);
mViewHolder= new MyViewHolder(v,type);
DrawerCustomList val = mList.get(position);
mViewHolder.itemImage.setImageResource(val.getmListImage());
mViewHolder.itemName.setText(val.getmListName());
}else {
v = LayoutInflater.from(getContext()).inflate(R.layout.drawer_header_layout,parent,false);
mViewHolder= new MyViewHolder(v,type);
DrawerCustomList val = mList.get(position);
mViewHolder.itemImage.setImageResource(val.getmListImage());
mViewHolder.itemName.setText(val.getmListName());
}
v.setTag(mViewHolder);
return v;
}
Change your MyViewHolder to this:
public static class MyViewHolder{
TextView itemName,headerName,headerEmail;
CircleImageView headerPicture;
ImageView itemImage;
public MyViewHolder(View v,int type){
if(type==vTypeHeader){
headerName = (TextView)v.findViewById(R.id.drawerHeaderName);
headerEmail = (TextView)v.findViewById(R.id.drawerHeaderEmail);
headerPicture = (CircleImageView)v.findViewById(R.id.drawerCircleImageView);
itemName = (TextView) v.findViewById(R.id.drawerItemName);
itemImage = (ImageView)v.findViewById(R.id.drawerItemImage);
}else {
itemName = (TextView) v.findViewById(R.id.drawerItemName);
itemImage = (ImageView)v.findViewById(R.id.drawerItemImage);
}
}
}
Hope this helps.
Upvotes: 0
Reputation: 3760
Seems like you're not accounting for the header
view in the getCount()
method.
First change the latter to:
@Override
public int getCount() {
return mList.size() + 1; // accounting for the header
}
Now keep in mind that in the current version of your getView()
method, if position == 0
you'll display the header
, which is correct. If position == 1
though, you'll do
....
if(type==vTypeItem){
v = LayoutInflater.from(getContext()).inflate(R.layout.drawer_single_item,parent,false);
mViewHolder= new MyViewHolder(v,type);
DrawerCustomList val = mList.get(position);
Notice how mList.get(position)
will actually get the second item in the list (with index 1), instead of the first one, which is what you need. So change this line to ...mList.get(position - 1)
and your problem should be solved.
Upvotes: 0