I have a Custom adapter
extending BaseAdapter
, everything works fine except the returned position of items in the getView
method which looks wrong. After the first scroll
of the list It gives me the index of the last displayed item.
First scroll top to bottom : 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12
First scroll bottom to top : 12 - 11 - 10 - 9 - 8 - 7 - 6
Second scroll top to bottom : 6 - 7 - 8 - 9 - 10 - 11 - 12
and starts to be pretty random.
This is my Adapter's code :
public abstract class FourComponentsAdapter extends BaseAdapter {
private UIViewImpl context;
private ArrayList<FourComponentsListItem> listItems;
private int layoutId;
private int itemPosition;
public FourComponentsAdapter (Context context, ArrayList<FourComponentsListItem> listItems, int layouId){
this.context = context;
this.listItems = listItems;
this.layoutId = layouId;
}
@Override
public int getCount () {
return listItems.size ();
}
@Override
public Object getItem (int position) {
return listItems.get (position);
}
@Override
public long getItemId (int position) {
return position;
}
@Override
public View getView (int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder viewHolder;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater ();
rowView = inflater.inflate (layoutId, null);
viewHolder = new ViewHolder ();
UIImage thumbnail = findThumbnail (rowView, viewHolder);
UILabel title = findTitle (rowView, viewHolder);
UILabel subTitle = findSubTitle (rowView, viewHolder);
UIIcon actions = findIconAction (rowView, viewHolder);
if (thumbnail != null) {
viewHolder.iconThumbnail = thumbnail;
}
if (title != null) {
viewHolder.title = title;
}
if (subTitle != null) {
viewHolder.subTitle = subTitle;
}
if (actions != null) {
viewHolder.iconAction = actions;
}
rowView.setTag (viewHolder);
} else {
viewHolder = (ViewHolder)rowView.getTag ();
}
itemPosition = position;
if (viewHolder.iconThumbnail != null) {
// Image or text
viewHolder.iconThumbnail.setOnClickListener (new OnClickListener () {
@Override
public void onClick (View view) {
onThumbnailClick (view, itemPosition);
}
});
}
if (viewHolder.title != null) {
viewHolder.title.setText (listItems.get (position).getTitle ());
viewHolder.title.setOnClickListener (new OnClickListener () {
@Override
public void onClick (View view) {
onTitleClick (view, itemPosition);
}
});
}
if (viewHolder.subTitle != null) {
viewHolder.subTitle.setText (listItems.get (position).getSubTitle ());
}
if (viewHolder.iconAction != null) {
viewHolder.iconAction.setText (listItems.get (position).getIconAction ());
viewHolder.iconAction.setTag (String.valueOf (position));
viewHolder.iconAction.setOnClickListener (new OnClickListener () {
@Override
public void onClick (View view) {
onActionClick (view, itemPosition);
}
});
}
System.out.println ("POSITION ::::: " + itemPosition);
return rowView;
}
protected abstract UIImage findThumbnail (View rowView, ViewHolder viewHolder);
protected abstract UILabel findTitle (View rowView, ViewHolder viewHolder);
protected abstract UILabel findSubTitle (View rowView, ViewHolder viewHolder);
protected abstract UIIcon findIconAction (View rowView, ViewHolder viewHolder);
protected abstract void onThumbnailClick (View view, int position);
protected abstract void onTitleClick (View view, int position);
protected abstract void onActionClick (View view, int position);
public static class ViewHolder {
public UIImage iconThumbnail;
public UILabel title;
public UILabel subTitle;
public UIIcon iconAction;
}
}
My Activity's code :
private ArrayList<FourComponentsListItem> listItems = new ArrayList<FourComponentsListItem> ();
....
UIListView listView = (UIListView) view.findViewById (R.id.list));
listView.setAdapter (
new FourComponentsAdapter (UserActivityView.this, listItems, listId) {
@Override
protected UIImage findThumbnail (View rowView, ViewHolder viewHolder) {
return (UIImage)rowView.findViewById (R.id.thumbnail);
}
@Override
protected UILabel findTitle (View rowView, ViewHolder viewHolder) {
return (UILabel)rowView.findViewById (R.id.name);
}
@Override
protected UILabel findSubTitle (View rowView, ViewHolder viewHolder) {
return (UILabel)rowView.findViewById (R.id.subName);
}
@Override
protected UIIcon findIconAction (View rowView, ViewHolder viewHolder) {
return (UIIcon)rowView.findViewById (R.id.actions);
}
@Override
protected void onThumbnailClick (View view, int position) {
}
@Override
protected void onTitleClick (View view, int position) {
}
@Override
protected void onActionClick (View view, int position) {
// Code here
}
);
Am I missing something ?? Thank you.