当我在Fragments Android之间导航时,我丢失了数据

时间:2013-07-31 08:18:58

标签: android android-fragments views

我不知道我做错了什么。好吧,我有一个包含listView和Fragment的Activity,当我按下listView中的一个项目时,它会用另一个片段替换该片段。所以问题是当我向我的片段添加一些视图并用另一个片段替换它时如果我回到我的第一个片段我没有找到我的视图添加。 有一些代码:

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

        View addReport = inflater
                .inflate(R.layout.add_report, container, false);

        ListView listView = (ListView) addReport.findViewById(R.id.list);
        // listView.addHeaderView(padding);
        listView.setDivider(new GradientDrawable(Orientation.LEFT_RIGHT, colors));
        listView.setDividerHeight(1);

        remplirListOfReport();
        ReportAdaptor reportSchedule = new ReportAdaptor(getActivity(),
                R.layout.report_item_format, listOfReports);
        listView.setAdapter(reportSchedule);
        // Inflate the root layout
        myInflater = (LayoutInflater) addReport.getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);

        uploadFiles = (LinearLayout) addReport.findViewById(R.id.upload_layout);
        picture = (Button) addReport.findViewById(R.id.picture);
        video = (Button) addReport.findViewById(R.id.video);
        vocale = (Button) addReport.findViewById(R.id.vocal);
        report = (EditText) addReport.findViewById(R.id.report_text);

        picture.setOnClickListener(buttonOnClickListener);
        video.setOnClickListener(buttonOnClickListener);
        vocale.setOnClickListener(buttonOnClickListener);

        return addReport;
    }

Button.OnClickListener buttonOnClickListener = new Button.OnClickListener() {
        private String fullPathAudio = "";
    @Override
    public void onClick(View v) {
        if (v == picture) {
            if(null != getGPSCoordonnee(getActivity())){
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, MEDIA_TYPE_IMAGE);
            }
        } else if (v == video) {
            if(null != getGPSCoordonnee(getActivity())){
                 Intent intent1 = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
                 intent1.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set
                 //the video image quality to high
                 startActivityForResult(intent1, MEDIA_TYPE_VIDEO);
            }

        } else if (v == vocale) {
            if(null != getGPSCoordonnee(getActivity())){
                getOutputMediaFile(MEDIA_TYPE_AUDIO);
                fullPathAudio = URL_START_WITH + File.separator + AUDIO_NAME;

                vocale.setText("Vocal");
                View audioView = myInflater.inflate(R.layout.picture_format,
                        uploadFiles, false);

                description = (TextView) audioView
                        .findViewById(R.id.picture_text);
                imageView = (ImageView) audioView
                        .findViewById(R.id.picture_image);
                edit = (Button) audioView.findViewById(R.id.picture_edit);
                date = (TextView) audioView.findViewById(R.id.picture_date);

                date.setText(TIME);
                description.setText("Audio");

                imageView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent intent2 = new Intent(getActivity(),
                                ShowVideo.class);
                        intent2.putExtra("videoName", fullPathAudio);
                        startActivity(intent2);
                    }
                });

                edit.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        editPictureNames(description);
                    }
                });

                uploadFiles.addView(audioView);

                Intent myIntent = new Intent(getActivity(),
                        AudioRecordingActivity.class);
                myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                myIntent.putExtra("path", fullPathAudio);
                getActivity().startActivity(myIntent);
            }
        } 
    }

};

我的活动中有一种替代碎片的方法:

@Override
    public void addReport(long id) {
        LEADERBOARD_FRAG_TAG = "REPORT";
        int show = 0;
        fragment = getSupportFragmentManager().findFragmentByTag(LEADERBOARD_FRAG_TAG);
        if(null == fragment){
            fragment = new AddReportFragment();
            show = 0;
        }else
            show = 1;
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        transaction.replace(R.id.myFragment, fragment, LEADERBOARD_FRAG_TAG);
        transaction.addToBackStack(null);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        transaction.commit();
    }

1 个答案:

答案 0 :(得分:0)

我认为您的列表视图在更换后会被回收。尝试将其移至Fragment。