片段数据加载每次调用片段

时间:2018-05-07 11:19:31

标签: android listview android-fragments bottomnavigationview

我正在创建一个具有底部导航的应用程序,我正在使用api检索数据。我正在调用api,并在listview方法的fragment中为oncreateview分配值。但问题是每次执行此方法时api会在每次调用片段时检索数据。我想要的是,当第一次调用片段时,数据应该只加载一次,而每隔一个时间列表视图应该只显示以前加载的数据,我不知道如何做到这一点,如果您知道其他一些方法,请提供帮助。

这就是我调用片段的方式

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        android.app.FragmentManager manager = getFragmentManager();
        switch (item.getItemId()) {
            case R.id.navigation_schedule:
                Fragment scheduleFragment = new ScheduleFragment();
                    manager.beginTransaction().replace(R.id.main, scheduleFragment).commit();
                return true;
            case R.id.navigation_teams:
                Fragment teamFragment = new TeamFragment();
                manager.beginTransaction().replace(R.id.main, teamFragment).commit();
                return true;
            case R.id.navigation_champions:
                Fragment championsFragment = new ChampionsFragment();
                manager.beginTransaction().replace(R.id.main, championsFragment).commit();
                return true;
            case R.id.navigation_live:
                Fragment liveFragment = new LiveFragment();
                manager.beginTransaction().replace(R.id.main, liveFragment).commit();
                return true;
            case R.id.navigation_records:
                Fragment recordsFragment = new RecordsFragment();
                manager.beginTransaction().replace(R.id.main, recordsFragment).commit();
                return true;
        }
        return false;
    }
};

这就是数据在片段

中的加载方式
public class ScheduleFragment extends Fragment {

private ListView lvSchedule;

customAdapterSchedule adapterSchedule;
List<BeanSchedule> listSchedule = new ArrayList<>();
float x1, x2, y1, y2;

private static final String url = "https://wc19.000webhostapp.com/ad/class/api.php?method=schedule";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


}

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_schedule, null);

    lvSchedule = (ListView)view.findViewById(R.id.lvSchedule);





        adapterSchedule = new customAdapterSchedule(this.getContext(), listSchedule);
        lvSchedule.setAdapter(adapterSchedule);
        apiCall();

    return view;
}

public void apiCall(){
    JsonArrayRequest scheduleReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    for (int i = 0; i < response.length(); i++) {
                        try {
                            JSONObject obj = response.getJSONObject(i);
                            BeanSchedule schedule = new BeanSchedule();

                           schedule.setTeam1name(obj.getString("team1name"));
                            schedule.setTeam2name(obj.getString("team2name"));
                            schedule.setTeam2img(obj.getString("team2img"));
                            schedule.setTeam1img(obj.getString("team1img"));
                            schedule.setDate(obj.getString("date"));
                            if(obj.getString("daynight").equals("0"))
                                schedule.setDaynight(" ");
                            else
                                schedule.setDaynight("Day/Night");

                            // Genre is json array


                            // adding movie to movies array
                            listSchedule.add(schedule);

                        } catch (JSONException e) {
                            e.printStackTrace();
                          //  Toast.makeText(getActivity(),"inside catch",Toast.LENGTH_LONG);

                        }


                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapterSchedule.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {


        }
    });

    AppController.getInstance().addToRequestQueue(scheduleReq);

}

}

1 个答案:

答案 0 :(得分:0)

最初收到数据后,阻止您设置标记的原因是什么?我不知道你的代码是怎么样的,你的问题也不是很精细,但这样的事情应该有用:

boolean data_loaded_once = false;

并在您的API回调函数中:

if(!data_loaded_once){
    data_loaded_once = true;
    // load data here
}

话虽如此,你可能根本不应该遇到这个问题。

编辑:将数据保存在其他地方:ListView适配器(临时)或SQLite。