尝试从ViewPager中的父活动访问片段方法

时间:2018-09-25 22:16:53

标签: java android android-fragments android-viewpager android-lifecycle

我有一个名为 CBT_practice 的活动,其中包含以下片段: objective_fragment subjective_fragment theory_fragment 。这些片段通过viewpager动态添加到活动中。

但是将Objective_fragment添加到viewpager之后,我遇到了问题。父活动(CBT_practice)使用LoaderManager进行sqlite db查询以检索问题列表,然后这些片段在其onCreateView生命周期中被片段访问,然后存储在变量中。

但是,只有在我最小化并还原了电话屏幕(即,活动被暂停然后恢复)之后,我才尝试从父活动(CBT_practice)中调用Objective_fragment(getScore())中的方法单击按钮后,我收到一条错误消息,指出列表的大小为0。当我不最小化并恢复活动时,不会发生这种情况

  

列表是派生的   来自父活动并通过OnCreateView方法分配

     

暂停并恢复活动后, listSize OnCreateView方法返回60,而以getScore()方法返回0

令人惊讶的是,列表存储在全局变量中,然后通过onCreateView方法进行分配

这里是Parent活动加载器回调,它从db获取问题列表:

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        progressBarLN.setVisibility(View.GONE);
        data.moveToFirst();

        while(!data.isAfterLast()){

            id = data.getString(data.getColumnIndex("_id"));
            topic_id_selected = data.getString(data.getColumnIndex("topic_id"));
            year_id_selected = data.getString(data.getColumnIndex("year_id"));
            ques_num = data.getString(data.getColumnIndex("question_num"));
            ques = data.getString(data.getColumnIndex("question"));
            ans = data.getString(data.getColumnIndex("answer"));
            ans_desc = data.getString(data.getColumnIndex("answer_desc"));
            opt_ans_id = data.getString(data.getColumnIndex("option_answer_id"));
            opt_1 = data.getString(data.getColumnIndex("option_A"));
            opt_2 = data.getString(data.getColumnIndex("option_B"));
            opt_3 = data.getString(data.getColumnIndex("option_C"));
            opt_4 = data.getString(data.getColumnIndex("option_D"));
            opt_5 = data.getString(data.getColumnIndex("option_E"));
            ques_type_id = data.getString(data.getColumnIndex("question_type_id"));
            selected_ans_id = data.getString(data.getColumnIndex("selected_answer_id"));

            QuestionAnswerList ques_item = new QuestionAnswerList(ques_num, ques, ans, ans_desc, opt_ans_id, opt_1, opt_2, opt_3,
                    opt_4, opt_5, ques_type_id, selected_ans_id, topic_id_selected, year_id_selected);
            topic_name = get_topic_name(topic_id_selected);
            ques_item.set_topic_name(topic_name);
            if(ques_item.opt_1!=null && ques_item.opt_2!=null && ques_item.opt_3!=null)
                ques_item.setOptions(isShuffleOpt);
            ques_ans_list.add(ques_item);
            //ques_ans_list contains the list of questions retrieved from the db
            data.moveToNext();

        }


        if(isShuffleQues) Collections.shuffle(ques_ans_list);
        viewPager = (ViewPager)findViewById(R.id.viewpager);
        //viewPager.setOffscreenPageLimit(ques_type_count);
        setUpViewPager(viewPager, ques_type);

        tabLayout = (TabLayout)findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

    }

这是父活动中的代码,该事件在单击按钮后调用getScore()方法

//get the objective_fragment
Fragment fragment = adapter.getItem(0);

//calls the getScore() method
((obj_fragment) fragment).getScore();

这是片段的OnCreateView

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            setHasOptionsMenu(true);

                View view = inflater.inflate(R.layout.fragment_obj_tv, container, false);
                preferences = this.getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);


                quesNoTV = (TextView) view.findViewById(R.id.quesNoTV);
                webView = (TextView) view.findViewById(R.id.webview);

                rad1 = (RadioButton) view.findViewById(R.id.opt1);
                rad2 = (RadioButton) view.findViewById(R.id.opt2);
                rad3 = (RadioButton) view.findViewById(R.id.opt3);
                rad4 = (RadioButton) view.findViewById(R.id.opt4);
                rad5 = (RadioButton) view.findViewById(R.id.opt5);
                radGrp = (RadioGroup) view.findViewById(R.id.radiogroup);


                rad1.setOnClickListener(radClickListener);
                rad2.setOnClickListener(radClickListener);
                rad3.setOnClickListener(radClickListener);
                rad4.setOnClickListener(radClickListener);
                rad5.setOnClickListener(radClickListener);

                submit = (Button) view.findViewById(R.id.submit);

                next.setOnClickListener(clickListener);
                previous = (Button) view.findViewById(R.id.previous);

                previous.setOnClickListener(clickListener);


                //getting the parent activity
                activity = (CBT_Practice) getActivity();

              //assigning the list from the parent activity to the fragment ques_list variable
                ques_list = activity.ques_ans_list;

                //listSize here is 60
                listSize = ques_list.size();
                ques_obj = ques_list.get(startIndex);
                show_question(ques_obj);



            return view;
        }

这是片段的getScore()方法

    public int getScore(){
        Intent intent;
        Bundle args = new Bundle();
        ArrayList<QuestionAnswerList> casted_list = new ArrayList<>(ques_list);
        int correct = 0;
        for(QuestionAnswerList item : ques_list){
            if(item.getPicked_ans_id()==item.get_shuffle_ans_id()&&item.getPicked_ans_id()!=-1) correct = correct+1;
        }
        int num = correct;
        correct = (correct*100)/listSize;
        //listSize here is 0


        if(activity.ques_type.get(0).equals("0")) {
            intent = new Intent(getActivity(), ResultActivity.class);
            args.putInt("score", correct);
            args.putInt("numOfQues", objective_list.size());
            args.putInt("correct", num);
        }
        else
            intent = new Intent(getActivity(), Answers.class);




        args.putStringArrayList("types", new ArrayList<>(activity.ques_type));
        args.putParcelableArrayList("list", casted_list);
        intent.putExtras(args);
        getActivity().startActivity(intent);
        return correct;
    }

我也有这个精确代码的副本,可以正常工作,但是由于我将Firebase添加到了项目中,所以我一直遇到错误。我不知道Firebase依赖项是否会导致错误。我也尝试过使用saveInstance状态并实现了某些活动生命周期,但无济于事

0 个答案:

没有答案