2015-06-03 78 views
2

我在我的列表視圖中使用自定義適配器。當我在我的數組列表中打印出我的數據時,它會正確存儲,但當我滾動項目時會重複。我知道我的getView方法有點混亂和混亂,但它爲什麼不正確地表示我的數據列表數據「oslist」?在ListView中滾動時重複的項目

這裏是我的適配器代碼:

public class WXYCAdapter extends BaseAdapter { 

private FragmentActivity context; 
private ArrayList<HashMap<String, String>> oslist; 
private ArrayList<HashMap<String, String>> heartList; 

private LayoutInflater mInflater; 

private final static int STREAM_LAYOUT = 0; 
private final static int TALKSET_LAYOUT = 1; 
private final static int BREAKPOINT_LAYOUT = 2; 
private final static int PLAYCUT_LAYOUT = 3; 
private final static int NULL_LAYOUT = 4; 

String URL; 
ViewHolder holder; 

public WXYCAdapter(FragmentActivity context, ArrayList<HashMap<String, String>> oslist) { 
    this.context = context; 
    this.oslist = oslist; 
    this.mInflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

} 


@Override 
public int getCount() { 
    return this.oslist.size(); 
} 

@Override 
public Object getItem(int position) { 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public int getItemViewType(int position) { 

    int RETURN_LAYOUT = NULL_LAYOUT; 

    switch (oslist.get(position).get("layoutType")) { 
     case "LiveStream": 
      RETURN_LAYOUT = STREAM_LAYOUT; 
      break; 

     case "Playcut": 
      RETURN_LAYOUT = PLAYCUT_LAYOUT; 
      break; 

     case "Talkset": 
      RETURN_LAYOUT = TALKSET_LAYOUT; 
      break; 

     case "Breakpoint": 
      RETURN_LAYOUT = BREAKPOINT_LAYOUT; 
      break; 
    } 

    return RETURN_LAYOUT; 

} 

@Override 
public int getViewTypeCount() { 
    return 4; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    holder = null; 
    int type = getItemViewType(position); 

    if (convertView == null) { 
     holder = new ViewHolder(); 
     switch (type) { 
      case STREAM_LAYOUT: 
       convertView = mInflater.inflate(R.layout.listview_cell, null); 

       holder.cell_image = (ImageView) convertView.findViewById(R.id.cell_image); 
       holder.cell_image.setImageResource(R.drawable.boombox); 

       holder.song = (TextView) convertView.findViewById(R.id.song); 
       holder.artist = (TextView) convertView.findViewById(R.id.artist); 

       holder.song.setEnabled(false); 
       holder.song.setMaxHeight(0); 

       holder.artist.setEnabled(false); 
       holder.artist.setMaxHeight(0); 

       convertView.setTag(holder); 
       break; 

      case TALKSET_LAYOUT: 
       convertView = mInflater.inflate(R.layout.listview_cell, null); 

       holder.cell_image = (ImageView) convertView.findViewById(R.id.cell_image); 
       holder.cell_image.setImageBitmap(null); 
       holder.cell_image.setEnabled(false); 
       holder.cell_image.setMaxHeight(0); 
       holder.cell_image.setMaxWidth(0); 

       holder.song = (TextView) convertView.findViewById(R.id.song); 
       holder.artist = (TextView) convertView.findViewById(R.id.artist); 
       holder.song.setText("Talkset"); 
       holder.artist.setText(null); 

       holder.artist.setEnabled(false); 
       holder.artist.setMaxHeight(0); 

       convertView.setTag(holder); 
       break; 

      case BREAKPOINT_LAYOUT: 
       convertView = mInflater.inflate(R.layout.listview_cell, null); 

       holder.cell_image = (ImageView) convertView.findViewById(R.id.cell_image); 
       holder.cell_image.setImageBitmap(null); 
       holder.cell_image.setEnabled(false); 
       holder.cell_image.setMaxHeight(0); 
       holder.cell_image.setMaxWidth(0); 

       holder.song = (TextView) convertView.findViewById(R.id.song); 

       //holder.song.setTypeface(Typeface.createFromAsset(context.getAssets(), "default.ttf")); 

       holder.artist = (TextView) convertView.findViewById(R.id.artist); 
       holder.song.setText("Breakpoint"); 
       holder.artist.setText(null); 
       holder.artist.setEnabled(false); 
       holder.artist.setMaxHeight(0); 


       long l = Long.parseLong(oslist.get(position).get("hour")); 

       Calendar calendar = Calendar.getInstance(); 
       calendar.setTimeInMillis(l); 
       calendar.setTimeInMillis(l * 1000); 

       int hour = calendar.get(Calendar.HOUR); 

       convertView.setTag(holder); 
       break; 

      case PLAYCUT_LAYOUT: //Playcut 


       convertView = mInflater.inflate(R.layout.listview_cell, null); 


       //holder.cell_image = (ImageView) convertView.findViewById(R.id.cell_image); 

       //holder.cell_image.setImageResource(R.drawable.no_album_art); 

       holder.song = (TextView) convertView.findViewById(R.id.song); 
       holder.artist = (TextView) convertView.findViewById(R.id.artist); 

       holder.song.setText(oslist.get(position).get("songTitle")); 
       holder.artist.setText(oslist.get(position).get("artistName")); 

       convertView.setTag(holder); 
       break; 
      case NULL_LAYOUT: 
       convertView.setTag(holder); 
       break; 

     } 


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

    return convertView; 

} 

@Override 
public boolean hasStableIds(){ 
    return true; 
} 



public static class ViewHolder { 
    public TextView textView, song, artist; 


    public ImageView cell_image; 
    public Button playButton; 
} 

}

+0

我很確定你的示例代碼可以從目前的198行(幾乎6K字符)中縮減。請參閱[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 – Jeeped

+0

我修好了!我只需將開關盒移到if-else語句下面。不完全確定爲什麼..但如果有人想解釋,我很樂意學習 –

回答

2

列表視圖應該重用的視圖。因此,相同的視圖可以用於列表中的其他項目。

的主要問題是,你正在改變視圖的內容只有當視圖是由於視圖可重複使用空

錯誤的方式

If(convertView == null) { 
    //Inflate view 
    // Change view content 
} 

,它並不總是空。

如上所述,在將switch語句移至空值條件之外後,問題就已修復。

這種方式,視圖內容將始終根據目前的項目(這是正確的)

正道

If(convertView == null) { 
    // inflate it 
} 
// Change view content 

這引起了混亂也向我一開始改變了... ... 查看被重用..但您應該相應地更改其內容。

+0

這個答案是現貨。我唯一要添加的就是在If(convertView == null){//膨脹它}'< - 視圖已創建,...以及在else {holder =(ViewHolder)convertView.getTag(); }'< - 視圖已被回收。將你的開關移到if/else語句之後,返回之前,你就可以走了。 – kev