2012-06-16 87 views
1

我想從使用notifyDatasetChanged()的位置偵聽器的onLocationChanged函數更新我的Activity的ListView,但它不起作用。我可以使用setListAdapter從我的代碼中的相同點,這將工作。notifydatasetchanged從內部偵聽器不工作

這裏是我的代碼:

public class TestListeners extends ListActivity { 

    ArrayAdapter adapter; 

    private final LocationListener networkLocationListener = new LocationListener() { 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      switch (status) { 
      case LocationProvider.AVAILABLE: 
       break; 
      case LocationProvider.OUT_OF_SERVICE: 
       break; 
      case LocationProvider.TEMPORARILY_UNAVAILABLE: 
       break; 
      } 
     } 

     @Override 
     public void onProviderEnabled(String provider) { 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
     } 

     @Override 
     public void onLocationChanged(Location location) { 
      checkProximity(location.getLatitude(), location.getLongitude(), cx); 
     } 
    }; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //get a list here venueNamesArray   
     //etc 

     adapter = new ArrayAdapter<String>(this, 
     android.R.layout.simple_list_item_1, android.R.id.text1, 
     venueNamesArray); 

     setListAdapter(adapter); 
    } 


    private void checkProximity(double curLat, double curLon, Context cx) { 

     //get a different list here venueNamesArray 
     //etc 

     adapter = new ArrayAdapter<String>(getApplicationContext(), 
       android.R.layout.simple_list_item_1, android.R.id.text1, 
       venueNamesArray); 

     adapter.notifyDataSetChanged();//this doesnt work and no error is thrown 
      setListAdapter(adapter);//this works 


    } 

} 

我想用notifyDatasetChanged(的原因),而不是setListAdapter因爲ListView的滾動回到頂端時,如果設定爲。當我使用notifyDatasetChanged()時,沒有錯誤拋出,所以我很難看出爲什麼這不起作用。

我已經嘗試使用正常的ListView而不是擴展ListActivity並遇到同樣的問題。

回答

2

因爲您每次都創建一個新的適配器。你不只是改變當前鏈接到列表的同一個適配器中的數據......

當你創建新的適配器和新的adter不會鏈接到列表,直到你調用setAdpter所以它是不值得調用adapter.notifyDataSetChanged();對於adtper不與列表鏈接...

解決方案

沒有必要每次都創建適配器。 jsut在On-create中創建並設置一次,只更改ArrayList(添加/刪除不創建新)並調用adapter.notifyDataSetChanged();

+0

其實我的問題是我用錯了類型的數組來填充我的ArrayList在這個線程看到http://stackoverflow.com/questions/3200551/unable-to-modify-arrayadapter-in-listview我已經錯誤地複製了新的ArrayAdapter初始值設定器 – brux

+0

yup,發生在每一個....... :) –

+0

感謝兄弟...它真的對我有幫助... –

1

在回撥中創建Adapter的新對象時,設置爲列表適配器的一個不會受到影響。因此在新實例上調用notifyDataSetChanged,並且與List關聯的那個不知道該方法調用。

更改代碼如下:

private void checkProximity(double curLat, double curLon, Context cx) { 

    //update the original venueNamesArray, don't create a new instance 

    adapter.notifyDataSetChanged(); 
} 
+0

其實我的問題是我使用錯誤類型的數組來填充我的ArrayList在這個線程看到http://stackoverflow.com/questions/3200551/unable-to-modify-arrayadapter-in-listview我已經無意中複製了新的ArrayAdapter初始值設定項 – brux

1

您在checkProximity重建適配器這就是爲什麼notifyDataSetChanged不工作。

將您的代碼更改爲以下內容。

private void checkProximity(double curLat, double curLon, Context cx) { 

     //get values that are to be added into venueNamesArray. 

     venueNamesArray.add(value1); //This is just sample you will put your values 
     venueNamesArray.add(value2); 


     adapter.notifyDataSetChanged(); 



    }