为什么我的ListView呈现重复的项目?

时间:2015-08-27 17:40:33

标签: java android arrays listview

这就是我填充ListView的方式:

这是情景:

1 - 创建两个数组

enter image description here

2 - 创建RowItem类

  public class IconRow {

  private String title;
  private int icon;

  public IconRow(String title, int icon) {
      this.title = title;
      this.icon = icon;

  }
      public String getTitle() {
      return title;
  }

  public int getIcon() {
      return icon;
  }

}

3 - 创建ListAdapter

public class ListAdapter extends BaseAdapter {

    Context context;
    List<IconRow> rowItem;
    String description;
    long option;

public ListAdapter(Context context, List<IconRow> rowItem, String description, long option)
{
    this.context = context;
    this.rowItem = rowItem;
    this.description = description;
    this.option = option;

}

@Override
public int getCount() {

    return rowItem.size();
}

@Override
public Object getItem(int position) {

    return rowItem.get(position);
}

@Override
public long getItemId(int position) {

    return rowItem.indexOf(getItem(position));
}

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

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

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


        ImageView imgIcon = (ImageView) convertView.findViewById(R.id.imageView1);
        TextView txtTitle = (TextView) convertView.findViewById(R.id.textView1);

        IconRow row_pos = rowItem.get(position);
        // setting the image resource and title
        imgIcon.setImageResource(row_pos.getIcon());
        txtTitle.setText(row_pos.getTitle());
    }

    return convertView;
}

4 - 创建行布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@android:color/white" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="10dp"
        android:fontFamily="sans-serif-light"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="24dp" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_gravity="right|center"
        android:layout_marginLeft="20dp"
        android:scaleType="fitEnd"
        android:src="@drawable/ic_go"
        android:layout_marginRight="0dp" />

</LinearLayout>

5 - 在片段

中创建列表
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_parameters_settings, container, false);

        lv_settings_1 = (ListView) rootView.findViewById(R.id.lv_settings_1);

        menutitles = getResources().getStringArray(R.array.array_lv_settings_1);
        menuIcons = getResources().obtainTypedArray(R.array.arrow_icons);

        menu_iconRow = new ArrayList<>();
        for (int i = 0; i < menutitles.length; i++) {
            IconRow items = new IconRow(menutitles[i], menuIcons.getResourceId(
                    i, -1));
            menu_iconRow.add(items);
        }

        adapter_settings_1 = new ListAdapter(getActivity(), menu_iconRow, "No Description", 0);

        lv_settings_1.setAdapter(adapter_settings_1);

        return rootView;
    }

问题

当在List上使用此方法最多7个或更多项时,它开始重复并填充错误。发生了什么事?

2 个答案:

答案 0 :(得分:2)

尝试更改适配器代码的这一部分:

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

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

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


    ImageView imgIcon = (ImageView) convertView.findViewById(R.id.imageView1);
    TextView txtTitle = (TextView) convertView.findViewById(R.id.textView1);

    IconRow row_pos = rowItem.get(position);
    // setting the image resource and title
    imgIcon.setImageResource(row_pos.getIcon());
    txtTitle.setText(row_pos.getTitle());

    return convertView;
}

转换视图可能会与旧值重复使用

答案 1 :(得分:1)

修正: 3 - 创建ListAdapter

    public class ListAdapter extends BaseAdapter {

   ...

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

        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

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

    }
            ImageView imgIcon = (ImageView) convertView.findViewById(R.id.imageView1);
            TextView txtTitle = (TextView) convertView.findViewById(R.id.textView1);

            IconRow row_pos = rowItem.get(position);
            // setting the image resource and title
            imgIcon.setImageResource(row_pos.getIcon());
            txtTitle.setText(row_pos.getTitle());
        }

        return convertView;
    }

了解ViewHolder。