2013-07-31 81 views
0

我不知道我在做什麼錯。以及我有一個活動,其中包含一個listView和一個片段,當我從我的listView中按項目時,它將替換與另一個片段的片段。所以問題是當我添加一些視圖到我的片段,並將其替換爲另一個片段,如果我返回到我的第一個片段,我沒有找到我的意見添加。 有一些代碼:當我在片段之間導航時丟失數據Android

@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(); 
    } 

回答

0

我相信這是更換後的列表視圖被回收。嘗試將其移至片段。

+0

感謝您的回覆,但什麼意思是「嘗試將其移動到片段」 –

+0

我的意思是創建另一個片段託管ListView和移動代碼管理它的數據到這個片段 – Chaosit