2012-08-11 34 views
0

我有一個約20項目的列表視圖。每行適配器都有一個圖像按鈕,當我點擊它時會執行不同的操作。當我點擊列表視圖的項目時,我設法正確地獲取位置編號。但是當我點擊圖像按鈕時,我得到的位置是錯誤的。而不是0〜19,但它顯示0 1 2 3 0 1 2 3 0 1 2 3 .....Android的listview postition序列錯誤

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    ViewHolder holder;   

    if (convertView == null) { 
     convertView  = myInflater.inflate(R.layout.list_video, null); 
     holder   = new ViewHolder(); 

     holder.rlContainer  = (RelativeLayout) convertView.findViewById(R.id.rlContainer); 
     holder.imageView  = (ImageView)  convertView.findViewById(R.id.ivLogo); 
     holder.buttonBackground = (ImageView)  convertView.findViewById(R.id.ibButtonLebih); 
     holder.tvtitle   = (TextView)  convertView.findViewById(R.id.tvTitle); 

     holder.buttonBackground.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) {    
       videoListClickListener.OnVideoMoreClickListener(position); 
      } 
     }); 

     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 


    return convertView; 
} 
+0

你應該把setTag的if/else語句 看到http://stackoverflow.com/a/5197476/661079 – 2012-08-11 02:06:00

+0

感謝肯尼之外!問題解決了! – Jason 2012-08-11 02:32:04

回答

1

問題解決!通過將onclicklistener和setTag移動到if/else之外。

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
ViewHolder holder;   

if (convertView == null) { 
    convertView  = myInflater.inflate(R.layout.list_video, null); 
    holder   = new ViewHolder(); 

    holder.rlContainer  = (RelativeLayout) convertView.findViewById(R.id.rlContainer); 
    holder.imageView  = (ImageView)  convertView.findViewById(R.id.ivLogo); 
    holder.buttonBackground = (ImageView)  convertView.findViewById(R.id.ibButtonLebih); 
    holder.tvtitle   = (TextView)  convertView.findViewById(R.id.tvTitle); 

} else { 
    holder = (ViewHolder) convertView.getTag(); 
} 

holder.buttonBackground.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) {    
      videoListClickListener.OnVideoMoreClickListener(position); 
     } 
    }); 

    convertView.setTag(holder); 


return convertView; 

}