【问题标题】:Row gets duplicated after delete from ListView从 ListView 删除后行被重复
【发布时间】:2013-12-13 19:54:46
【问题描述】:

我知道已经有很多关于这个问题的问题。

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

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

代码如下:

ListView 的适配器:

类 MyAdapter 扩展 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


    【解决方案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);
    

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

    所以,在 Adapter 类中创建一个方法并传递这个新的数组列表。

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

    那么你不需要打电话给adapter.notifyDataSetChanged(); 致电adapter.methodName(newArraylist)

    【讨论】:

    • 刚才试过你的建议,但还是不行。与之前的行为相同。包括这个:void updateData(ArrayList&lt;ListModel&gt; newList) { this.itemsArrayList = newList; notifyDataSetChanged(); } 并在更改数据集后在适配器上调用此方法。
    • 另外,列表视图确实会更新,只是在删除所选行后,列表中的最后一行会被复制以覆盖空间。
    • 还有inflater.inflate(R.layout.viewlistitems, null);
    • 也试过了。扩展 BaseAdapter,增加相关方法。仍然是相同的行为。这太烦人了,似乎没有任何效果..
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我有同样的问题,我从 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>
      

      所以正如我所说,我的问题是关于高度,希望你也是,和平。

      【讨论】:

        猜你喜欢
        • 2011-11-29
        • 1970-01-01
        • 2021-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-08
        • 1970-01-01
        相关资源
        最近更新 更多