ListAdapter如何知道它的Detached?

时间:2016-01-29 14:13:06

标签: java android listview gridview listadapter

firstAdapter如何知道它何时与listView的实际适配器分离(因为secondAdapter取代了它?)

final ListAdapter firstAdapter = new SimpleAdapter(
    this,
    new ArrayList<Map<String, Object>>(),
    android.R.layout.simple_list_item_1,
    new String[] {"AAA"},
    new int[] {android.R.id.text1}
);
final ListAdapter secondAdapter = new SimpleAdapter(
    this,
    new ArrayList<Map<String, Object>>(),
    android.R.layout.simple_list_item_1,
    new String[] {"AAA"},
    new int[] {android.R.id.text1}
);

setListAdapter(firstAdapter);

(new Timer()).schedule(new TimerTask() {
  @Override
  public void run() {
    Runnable r = new Runnable() {
      @Override
      public void run() {
        setListAdapter(secondAdapter);
      }
    };
    new Handler(Looper.getMainLooper()).post(r);
  }
}, 1000);

1 个答案:

答案 0 :(得分:0)

当第一个适配器从ListView取消注册时会收到通知,所以我使用的是:https://developer.android.com/reference/android/widget/Adapter.html#unregisterDataSetObserver(android.database.DataSetObserver)

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
  super.unregisterDataSetObserver(observer);

  Log.d("Adapter just got unregistered from the listView!");
  [...]
}