You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用notifyDataSetChanged后自定义Adapter显示错误项求助

Hey there, let's troubleshoot this frustrating ListView display issue you're facing! It sounds like your data source is updating correctly but the Adapter isn't reflecting those changes properly. Here are the most common fixes to try:

1. Make sure your Adapter uses the same ArrayList instance as your external code

A super common mistake is creating a copy of the ArrayList inside your Adapter's constructor instead of referencing the original one. For example, if you do something like mItems = new ArrayList<>(items); in the Adapter, any changes you make to the original list won't be picked up by the Adapter—because they're two separate objects.

Fix: Pass the original ArrayList directly to the Adapter, no copying. If you need to refresh the list later, add a method in your Adapter to update the internal reference and trigger the refresh:

public class YourAdapter extends BaseAdapter {
    private ArrayList<String> mItemList;

    public YourAdapter(ArrayList<String> itemList) {
        // Use the original list reference, don't create a new one!
        mItemList = itemList;
    }

    // Optional method to update the list later
    public void setItemList(ArrayList<String> newList) {
        mItemList = newList;
        notifyDataSetChanged();
    }

    // ... rest of your Adapter code ...
}

2. Double-check your getCount() method

If your Adapter's getCount() is returning a hardcoded number (like 3) instead of the actual size of your ArrayList, it'll keep trying to render old positions even after you remove items. For example, if getCount() still returns 3 after you remove one item, the ListView will try to show 3 entries—but your list only has 2, so it'll reuse old views from the first two positions instead of updating to show Item 2 and 3.

Fix: Make sure getCount() always returns the current size of your data list:

@Override
public int getCount() {
    return mItemList != null ? mItemList.size() : 0;
}

3. Verify you're removing the correct item and notifying the Adapter properly

In your onItemLongClick method, confirm that you're removing the item at the correct position, and that you're calling notifyDataSetChanged() on the same Adapter instance attached to your ListView. Here's how that should look:

@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
    // Remove the item using the position from the ListView click
    yourArrayList.remove(position);
    // Notify the Adapter to refresh the UI
    yourAdapter.notifyDataSetChanged();
    return true;
}

Quick sanity check: After removing the item, log the size of your ArrayList and the Adapter's getCount() value—they should match! If they don't, that means your Adapter isn't using the updated list.


内容的提问来源于stack exchange,提问作者Wiktor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:57:29