以下是一些测试代码,用于重现一个奇怪的错误:从 ListView 中删除一些项目后,当数据无效时它会停止刷新。更多项目被删除,但列表不刷新。甚至 Log cat 也不会显示用于删除的调试消息。如果有人能找出问题所在,我将不胜感激。
物品布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        android:orientation="horizontal">
    <TextView android:id="@+id/nameTextView"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              />
    <Button android:id="@+id/deleteButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Delete"
            />
</LinearLayout>
物品类别:
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Item implements View.OnClickListener {
    private String name;
    private View itemView;
    private MyActivity owner;
    //--- getters--
    public String getName() {
        return name;
    }
    public View getView() {
        return itemView;
    }
    public Item(String n, Context c , MyActivity o)
    {
        //---store the name given--
        name = n;
        //---store reference to the owner activity--
        owner = o;
        //--- create a View for this item----
        LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        itemView = inflater.inflate(R.layout.item,null);
        //---set up data to show--
        TextView nameTextView = (TextView) itemView.findViewById(R.id.nameTextView);
        Button deleteButton = (Button) itemView.findViewById(R.id.deleteButton);
        nameTextView.setText(name);
        //---set up events to be handled--
        deleteButton.setOnClickListener(this);
        Log.d("My_Test","Item: Hello world, my name is " + name);
    }
    //----request owner to delete this item---
    @Override
    public void onClick(View view) {
        Log.d("My_Test","Item:"+name+" requesting owner to delete me");
        owner.deleteItem(this);
    }
活动布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
<ListView android:id="@+id/myListView"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          />
</LinearLayout>
活动类:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MyActivity extends Activity {
    private ArrayList<Item> myItems;
    private ListView myListView;
    private ArrayAdapter<Item> myArrayAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //-----adapter for item list----
        //----since each item has its own view , it just returns the same---
        myArrayAdapter = new ArrayAdapter<Item>(this,0){
            @Override
            public View getView(final int position, View convertView, ViewGroup parent)      {
              Item item = getItem(position);
              Log.d("My_Test","Adapter : View for Item: " + item.getName() +"is requested." );
              return item.getView();
            }
        };
        //-----set up my list view with the adapter------
        myListView = (ListView) findViewById(R.id.myListView);
        myListView.setAdapter(myArrayAdapter);
        //------add items-------
        //----each item has its own view and a reference to this activity as their owner----
        myArrayAdapter.add(new Item("Sunday", this, this));
        myArrayAdapter.add(new Item("Monday", this, this));
        myArrayAdapter.add(new Item("Tuesday", this, this));
        myArrayAdapter.add(new Item("Wednesday", this, this));
        myArrayAdapter.add(new Item("Thursday", this, this));
        myArrayAdapter.add(new Item("Friday", this, this));
        myArrayAdapter.add(new Item("Saturday", this, this));
        myArrayAdapter.notifyDataSetChanged();
    }
    //----- called by items requesting to be deleted from the item list----
    public void deleteItem(Item item) {
        myArrayAdapter.remove(item);
        Log.d("My_Test","Owner : Deleted item :" + item.getName());
        myArrayAdapter.notifyDataSetChanged();
    }
}
看起来 ListView 停止重新绘制它自己。即使列表项不再在项数组中并被myAdapter.notifyDataSetInvalidated();调用,列表项仍然可见,进一步的代码执行有些受阻。