java - 在 ListView 中滚动时重复项目

标签 java android listview scroll adapter

我在 ListView 中使用自定义适配器。当我在数组列表中打印数据时,它会正确存储,但当我滚动项目时会重复。我知道我的 getView 方法有点困惑和困惑,但为什么它不能正确表示我的数组列表数据“oslist”?

这是我的适配器代码:

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

}

最佳答案

Listview 应该重用 View 。因此,相同的 View 可以用于列表的其他项目。

主要问题是只有当 View 为空时才更改 View 的内容

错误的方式

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

由于 View 可以重用,因此它并不总是为空。

正如您所说,将 switch 语句移至 null 条件之外后,问题已得到解决。

这样, View 内容将始终根据当前项目进行更改(这是正确的)

正确的方法

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

这在一开始也让我感到困惑...... View 被重用。但是您应该相应地更改其内容。

关于java - 在 ListView 中滚动时重复项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30631138/

相关文章:

c# - 如何在ListView中选择项目?

java - java中如何处理斜杠字符

java - 如何在android中使用unicode显示阿拉伯字符?

Android StartActivity(Intent) -> NullPointerException(与onclicklistener有关)

android - NativeScript 中的适用于 Android 的 OneSignal。不会加载库

android - 如何使用 Espresso 将时间设置为 MaterialDateTimePicker

android - 如何在项目点击 Activity 中调用两个 fragment

.net - 寻找 .NET 和 Mono 的 UI 库

java - 如何提高变长 Neo4j Cypher 查询的性能?

java - Maven WebApp 中的 Jetty SERVICE_UNAVAILABLE 与 Eclipse 中的 Jersey JAX-RS