从ListView中删除后,行会重复

问题描述 投票:0回答:3

我知道有很多关于同一问题的问题。

我看起来有点奇怪!我有一个ListView的自定义适配器,我传递了Model的ArrayList。我为此列表设置了一个上下文菜单,在单击删除时,我需要从ListView中删除该行(然后刷新)。

我调用xxxxx.remove(info.position)然后调用listadapter.notifyDataSetChanged(),但它复制了ListView中存在的最后一行。

这是代码:

ListView适配器:

class MyAdapter extends ArrayAdapter {

private final Context context;
private final ArrayList<ListModel> itemsArrayList;

public MyAdapter(Context context, ArrayList<ListModel> itemsArrayList) {

    super(context, R.layout.viewlistitems, itemsArrayList);
    this.context = context;
    this.itemsArrayList = itemsArrayList;
}

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

    View rowView = null;

    if (convertView == null) {

    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //Get rowView from inflater
    rowView = inflater.inflate(R.layout.viewlistitems, parent, false);
    }
    else
        rowView = convertView;

    // 3. Get the two text view from the rowView
    TextView listNameView = (TextView) rowView.findViewById(R.id.listName);

    listNameView.setTypeface(ViewListActivity.segoe);
    listDateView.setTypeface(ViewListActivity.openSansLightIt);

    String listName1 = itemsArrayList.get(position).getListName();
    String listName2 = listName1.substring(0,1).toUpperCase() + listName1.substring(1);


    listNameView.setText(listName2);

    return rowView;
}

}

上下文菜单:

public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
      if (v.getId()==R.id.ListView01) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        menu.setHeaderTitle(todoItems.get(info.position).getListName());
        String[] menuItems = {"Delete"};
        for (int i = 0; i<menuItems.length; i++) {
          menu.add(Menu.NONE, i, i, menuItems[i]);
        }
      }
    }

public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
            .getMenuInfo();

    if(item.getTitle().equals("Delete")) {
    Toast.makeText(this,
            todoItems.get(info.position).getListName() + " deleted!",
            Toast.LENGTH_SHORT).show();

    todoItems.remove(info.position);

    //newList.invalidateViews();
    adapter.notifyDataSetChanged();

    }
     return true;
    }

从1天开始无法修复此错误!知道为什么会发生这种情况或导致它的原因吗?

非常感谢!

android listview android-listview notifydatasetchanged
3个回答
1
投票

而不是这个:

if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // Get rowView from inflater
    rowView = inflater.inflate(R.layout.viewlistitems, parent, false);
    }
    else
        rowView = convertView;

试试这个:

LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// Get rowView from inflater
rowView = inflater.inflate(R.layout.viewlistitems, parent, false);

并且你删除项目然后不告诉Adapter类你删除项目因为适配器项目与temsArrayList相同

因此,在Adapter类中创建一个方法并传递这个新的arraylist。

private viod methodName(arraylist list)
{
temsArrayList=list;
notifyDataSetChanged();
}

然后你不需要打电话adapter.notifyDataSetChanged();呼叫adapter.methodName(newArraylist)


0
投票

好吧,我终于解决了这个问题。我使用notifyDataSetInvalidated()使当前适配器无效,并使用我修改的列表设置新的适配器。工作得很好。


0
投票

我有同样的问题,我正在寻找解决方案,因为2天。我终于发现它是一个XML问题而不是java问题。问题是在ListView高度,它的值为“wrap_content”,我将其更改为“match_parent”。问题解决了。然后我找到了一个更好的解决方案,最好将ListView放在LinearLayout中。

<LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <ListView
                        android:id="@+id/list_topups"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"/>
                </LinearLayout>

所以我说我的问题是关于身高,希望你的问题,和平。

© www.soinside.com 2019 - 2024. All rights reserved.