Reputation: 11
I am beginner to android..i created four images and below each image i added text for that image i added renaming option by onlong click of text..for storing renamed text i am using shared prefrences in my fragment..if i go next fragment and if i come back my fragment edited text is not displaying..its displaying what i added text in layout..can anyone help me....below is my code
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;
}
});
Upvotes: 0
Views: 66
Reputation: 2535
In your onCreateView of fragment write following code it will work:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String imgUpdateName = prefs.getString("Str", "");
text1.setText(imgUpdateName);
so your onCreateView will be :
@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.
Upvotes: 1
Reputation: 3887
Here you go! Add this on your fragment code!
@Override
public void setUserVisibleHint ( boolean isVisibleToUser){
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
yourTextView.setText(""+yourSharedPrefObject.getString("Str",""));
}
}
Upvotes: 0