繁体   English   中英

notifydataset在侦听器内部无法正常工作

[英]notifydatasetchanged from within listener not working

我正在尝试使用notifyDatasetChanged()从位置侦听器的onLocationChanged函数更新我的Activity的ListView,但它不起作用。 然而,我可以在我的代码中的同一点使用setListAdapter,这将起作用。

这是我的代码:

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并遇到了同样的问题。

因为您每次都在创建一个新的适配器。 您不仅要更改当前链接到列表的同一个适配器中的数据........

在创建新适配器时,新的adter不会与list链接,直到你调用setAdpter所以不值得调用adapter.notifyDataSetChanged(); 对于未与列表链接的adtper ...

每次都不需要创建适配器。 jsut在On-create中创建并设置一次,只更改ArrayList(添加/删除不创建新)并调用adapter.notifyDataSetChanged();

在回调中创建Adapter的新对象时,设置为列表适配器的对象不会受到影响。 因此,在新实例上调用notifyDataSetChanged ,与List关联的那个不知道方法调用。

将代码更改为以下内容:

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

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

    adapter.notifyDataSetChanged();
}

您正在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();



    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM