代码之家  ›  专栏  ›  技术社区  ›  electronix384128

ListAdapter如何知道何时分离?

  •  0
  • electronix384128  · 技术社区  · 8 年前

    如何 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 回复  |  直到 8 年前
        1
  •  0
  •   electronix384128    8 年前

    结果第一个Adapter在从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!");
      [...]
    }