我在我的列表視圖中使用自定義適配器。當我在我的數組列表中打印出我的數據時,它會正確存儲,但當我滾動項目時會重複。我知道我的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;
}
}
我很確定你的示例代碼可以從目前的198行(幾乎6K字符)中縮減。請參閱[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 – Jeeped
我修好了!我只需將開關盒移到if-else語句下面。不完全確定爲什麼..但如果有人想解釋,我很樂意學習 –