从Activity返回后,在Fragment中重新加载ListView

时间:2016-10-04 09:22:27

标签: android listview android-fragments

我有一个从数据库加载ListView的Fragment,它是用户之前选择的收藏夹列表,当您单击列表项时,它启动一个Activity,用户将能够看到详细信息并且不喜欢该项目问题是当用户不喜欢某个项目并按回到收藏列表的片段时,该视图仍然显示第一个嵌入的列表视图而不是更新的列表视图。我想知道当你开始一个活动时片段的生命周期是如何工作的,然后又回来知道在哪里实现我的

adapter.notifyDataSetChanged()

我使用GoogleApiClient获取用户的位置,因此我用我的onConnected方法加载数据库:

    @Override
public void onConnected(@Nullable Bundle bundle) {

    LocationServices.FusedLocationApi.requestLocationUpdates(
            mLocationClient, mLocationRequest, this);

    if (    ContextCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // You need to ask the user to enable the permissions
    } else {
        LocationTracker tracker = new LocationTracker(getContext()) {
            @Override
            public void onLocationFound(@NonNull Location location) {
                currentLocation = location;
            }
            @Override
            public void onTimeout() {
            }
        };
        tracker.startListening();
    }
    if (currentLocation ==null) {
        currentLocation = LocationServices.FusedLocationApi.getLastLocation(mLocationClient);
    }

    if (ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
                PERMISSION_ACCESS_COARSE_LOCATION);
    }
    if (currentLocation != null) {
        //Loading LAT LOng of //arkers on map
        for(int i=0; i<storedfavoritesitem.size(); i++) {
            FavoriteModel Faveitem = storedfavoritesitem.get(i);
            String locality = Faveitem.name;
            int id = Faveitem.id;
            double lat = Faveitem.lat;
            double lon =  Faveitem.lon;
            Location location = new Location("Beach");
            location.setLatitude(lat);
            location.setLongitude(lon);
            float distance = currentLocation.distanceTo(location);
            mResualt.add(new SearchResualtClass(id, distance / 1000, locality));
        }
        Comparator<SearchResualtClass > comperator = new Comparator<SearchResualtClass>()
        {
            @Override
            public int compare(SearchResualtClass  object1, SearchResualtClass  object2) {
                float First = object1.getDistance();
                float Second = object2.getDistance();
                return (First < Second ? -1 : (First == Second ? 0 : 1));
            }
        };
        Collections.sort(mResualt, comperator);
        adapter = new CustomAdapterSearch(getActivity().getApplicationContext(),mResualt);
        exList.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    exList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            Intent intent = new Intent(view.getContext(), FragmentBeach.class);
            intent.putExtra("array", jsonArray.toString());
            SearchResualtClass item = (SearchResualtClass) parent.getItemAtPosition(position);
            intent.putExtra("id", item.getId());
            startActivity(intent);
        }
    });
}

3 个答案:

答案 0 :(得分:0)

移动加载了数据列表的代码,并将其从onCreate()发送到适配器onResume()

这将确保只要片段经历暂停 - 恢复阶段,您的列表就会重新加载。

就生命周期而言,请阅读[Fragment Lifecycles] [** 1]

更新:您必须更新storedfavoritesitem 然后运行以下代码

if (currentLocation != null) {
        //Loading LAT LOng of //arkers on map
        for(int i=0; i<storedfavoritesitem.size(); i++) {
            FavoriteModel Faveitem = storedfavoritesitem.get(i);
            String locality = Faveitem.name;
            int id = Faveitem.id;
            double lat = Faveitem.lat;
            double lon =  Faveitem.lon;
            Location location = new Location("Beach");
            location.setLatitude(lat);
            location.setLongitude(lon);
            float distance = currentLocation.distanceTo(location);
            mResualt.add(new SearchResualtClass(id, distance / 1000, locality));
        }
        Comparator<SearchResualtClass > comperator = new Comparator<SearchResualtClass>()
        {
            @Override
            public int compare(SearchResualtClass  object1, SearchResualtClass  object2) {
                float First = object1.getDistance();
                float Second = object2.getDistance();
                return (First < Second ? -1 : (First == Second ? 0 : 1));
            }
        };
        Collections.sort(mResualt, comperator);
     if(adapter == null){
        adapter = new CustomAdapterSearch(getActivity().getApplicationContext(),mResualt);
        exList.setAdapter(adapter);
    }else{
        adapter.notifyDataSetChanged();
    }

答案 1 :(得分:0)

adapter.notifyDataSetChanged()肯定会起作用。 您必须使用startActivityForResult方法调用活动,并在调用活动完成之前传递resultCode。基于此resultCode,您可以调用adapter.notifyDataSetChanged()。 这是示例代码。请相应更换。

startActivityForResult(intent, requestCode);

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == 1) {
        adapter.notifyDataSetChanged();
    }
}

答案 2 :(得分:0)

好吧我解决了,我是notifyDataSetChanged但实际上所有内容都是从数据库加载所以在我做了更改之后我应该再次从数据库重新加载列表并发送更改通知,以便我在{{1}中实现以下代码}}

OnResume

现在感谢每一个