2016-11-25 66 views
0

我是初學者android..i創建四個圖像,並在每個圖像下面我添加文本 爲該圖像我添加了重命名選項通過長時間點擊文本..for存儲改名 文本我使用共享prefrences我fragment..if我,我去下一個片段,如果我 回來我的片段編輯的文本不displaying..its顯示我加在layout..can文字的人幫我....下面是我的代碼可編輯的文本和存儲該文本在Android

 public class MyFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.ac_listview, container, false); 

    final TextView text1 = (TextView) view.findViewById(R.id.textView8); 

      ImageView image1 = (ImageView) view.findViewById(R.id.imageView3); 


    image1.setOnClickListener(new Button.OnClickListener() { 
     @Override 
     public void onClick(View _view) { 
      int viewId = _view.getId(); 
      FragmentTransaction ft; 
      switch (viewId) { 
       case R.id.imageView3: 
        FragmentManager fm = getFragmentManager(); 
        ft = fm.beginTransaction(); 
        NEXTFRAGMENT nextFragment = new NEXTFragment(); 

        ft.replace(R.id.frame_content, nextFragment); 
        ft.addToBackStack(null); 
        ft.commit(); 
        break; 

      } 


     } 
    }); 

    text1.setText("green"); 

    text1.setOnLongClickListener(new View.OnLongClickListener() { 
     @Override 
     public boolean onLongClick(View view) { 
      LayoutInflater inflater = ((Activity) getActivity()).getLayoutInflater(); 

      View view = inflater.inflate(R.layout.name_edit, null); 
      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity()); 

      alertDialogBuilder.setView(view); 

      final AlertDialog alertDialog = alertDialogBuilder.create(); 

      Button done = (Button) View.findViewById(R.id.edit_done); 
      final TextView new1 = (TextView) View.findViewById(R.id.name); 

      done.setOnClickListener(new Button.OnClickListener() { 
       @Override 
       public void onClick(View view) { 

        String str = new1.getText().toString(); 

        if(str.equals("")) 
        { 
         new1.setError(Constants.NOT_EMPTY); 

        } 
        else{ 
         text1.setText(str); 
         SharedPreferences.Editor editor = MAINActivity._sharedPreferences.edit(); 
         editor.putString("Str", str); 
         editor.commit(); 
         alertDialog.dismiss(); 
        } 

       } 

      }); 
      alertDialog.show(); 

      return true; 

     } 


    }); 
+0

您應該從SharedPreferences中獲取onCreateView的TextView上的文本。按照@Anjali的答案。 –

回答

1

在你的下面的代碼片段寫onCreateView它會工作:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
String imgUpdateName = prefs.getString("Str", ""); 
text1.setText(imgUpdateName); 

所以你onCreateView將是:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.ac_listview, container, false); 

    final TextView text1 = (TextView) view.findViewById(R.id.textView8); 

      ImageView image1 = (ImageView) view.findViewById(R.id.imageView3); 
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    String imgUpdateName = prefs.getString("Str", ""); 
    text1.setText(imgUpdateName); 

....... 

Your remaining fragment code. 
+0

anjali我試過同樣的問題,我不知道我做了什麼錯誤...你可以發送完整的代碼 –

+0

你可以發送完整的代碼 –

0

在這裏你去!將這添加到你的片段代碼中!

@Override 
    public void setUserVisibleHint (boolean isVisibleToUser){ 
     super.setUserVisibleHint(isVisibleToUser); 
     if (isVisibleToUser) { 
      yourTextView.setText(""+yourSharedPrefObject.getString("Str","")); 
     } 
    }