2014-05-15 98 views
0

我有一個Custom adapter延伸BaseAdapter,一切正常,除了getView方法中的項目返回位置看起來不正確。列表後的first scroll它給了我最後顯示的項目的索引。獲取錯誤的項目位置ListView

首先滾動從上到下:0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12

首先滾動自下而上:12 - 11 - 10 - 9 - 8 - 7 - 6

第二滾動從上到下:6 - 7 - 8 - 9 - 10 - 11 - 12

,開始是相當隨機的。

這是我的適配器代碼:

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; 
} 
} 

我的動態代碼:

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 
     } 
    ); 

我缺少的東西?謝謝。

+0

您是否有6個可見項? 僅當項目變得可見時調用'getView'。所以我沒有看到任何問題... – Suvitruf

+0

你添加了任何headerview到你的列表視圖 –

+0

顯示你所說的「漂亮隨機」 – tana

回答

0

請勿將變量itemPosition用作類成員。 要在onClickListener中使用position,請聲明它爲final。

此外,如果你不使用getItemgetItemId,他們應該分別返回null0

PS:相信ListView,它知道它需要哪個視圖:)

+0

我使用'itemPosition'作爲類成員以避免使用'final' ...我不'真的明白你的觀點。 – Copernic

+0

當ListView再次調用getView時,itemPosition被覆蓋。因此,當OnClickListener被觸發時,它會讀取itemPosition,這對您的列表視圖中的每個項目都是相等的(即等於適配器創建的最後一個視圖的位置)。 – Spotlight

+0

謝謝。我知道這一切..我在這裏發佈的理由是,我得到了一個非預期的行爲,我在問題中解釋了這一點。 – Copernic

相關問題