如何在嵌套片段的backpressed中更新listview?

时间:2018-07-01 10:39:15

标签: android-fragments

这是我的片段,其中有一个列表视图,并使用ChapterListAdapter填充它。

课程片段

    public class CurriculumFragment extends Fragment implements 
ResponseDelegate {
        Context contextd;
        ListView curriculumList;
        FragmentActivity mActivity;
        boolean onResult=false;
        int requestCode, resultCode;
        Intent data;
        String courseId;
        private RequestedServiceDataModel requestedServiceDataModel;
        private ArrayList<ChapterItem> courses = new ArrayList<>();
        private ChaptersListAdapter adapter;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            contextd = container.getContext();
            View rootView = inflater.inflate(R.layout.fragment_circulam, container, false);
            courseId = getArguments().getString("course_id","2");
            curriculumList = (ListView) rootView.findViewById(R.id.circulamList);
            adapter = new ChaptersListAdapter((AppCompatActivity) getActivity(), courses);
            curriculumList.setAdapter(adapter);   
            serverRequestForCompleteModule();  // This function fill arraylist courses with data.
            return rootView;
        }
        @Override
        public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (FragmentActivity) activity;
        }

        @Override
        public void onDetach() {
        super.onDetach();
        contextd = null;
        mActivity = null;
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent 
data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == getActivity().RESULT_OK && requestCode == 1111)
        {
        courses.clear();
        courses = (ArrayList<ChapterItem>) data.getSerializableExtra("list");
        adapter = new ChaptersListAdapter((AppCompatActivity)mActivity, 
courses);
        curriculumList.setAdapter(adapter);
        }
    }

ChapterListAdapter的OnItemclick我正在这样打开Activity MediaplayerActivity3

ChapterListAdapter

     void dialogMediaPlayer(final ArrayList<ChapterItem.Module> mlist, final int position, String title, String imageUrl,String chapname,boolean audioStream) {

        Intent mint = new Intent(contextd, MediaPlayerActivity3.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("courselist",list);
        bundle.putSerializable("list",mlist);
        bundle.putInt("gpos",glPos);
        bundle.putInt("pos",position);
        bundle.putString("title",title);
        bundle.putString("image",imageUrl);
        bundle.putString("chapter",chapname);
        bundle.putBoolean("stream",audioStream);
        mint.putExtras(bundle);
        contextd.startActivityForResult(mint,1111);
        contextd.overridePendingTransition( R.anim.slide_up, R.anim.slide_down );
}

现在我在“活动”中做我的工作,现在当我按下按键时,我想更新课程片段中的listView,这是通过OnActivityResult方法执行的,但在onActivityResult方法中,getActivity()始终返回null。

活动中的Onbackpressed方法如下

MediaPlayerActivity3

public class MediaPlayerActivity3 extends AppCompatActivity implements ResponseDelegate {
               @Override
public void onBackPressed() {
    saveInsatance(tmpModule.get(position).getModule_id());
    stop = false;
    myHandler.removeCallbacks(UpdateSongTime);

    int x1=0;
    if(tmpModule.get(posit).getIs_module_comeplete().equals("0")){
        x1=(int)(TimeUnit.MILLISECONDS.toSeconds((long)finalTime)-TimeUnit.MILLISECONDS.toSeconds((long)startTime));
        if(x1<5){
            serverRequestQuizForAudeoComplite(tmpModule.get(posit).getModule_id());
            tmpModule.get(posit).setIs_module_comeplete("1");

        }
    }
    courses.get(glpos).setModule_files(tmpModule);
    Intent returnIntent = new Intent();
    Bundle bundle = new Bundle();
    bundle.putSerializable("list",courses);
    bundle.putString("complete",completed);
    returnIntent.putExtras(bundle);
    setResult(Activity.RESULT_OK,returnIntent);
    super.onBackPressed();
 }
}

0 个答案:

没有答案