Showing error for MODE_PRIVATE within fragment in android - android

Showing error while using MODE_PRIVATE for sharedPreferences within onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.activity_package_categories, container, false);
SharedPreferences pref = getActivity().getSharedPreferences("PackageType", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("package_categories_id", position);
editor.commit();
return layout;
}

You should use it like this Context.MODE_PRIVATE.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.activity_package_categories, container, false);
SharedPreferences pref = getActivity().getSharedPreferences("PackageType", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("package_categories_id", position);
editor.commit();
return layout;
}

Add context in your code like :
SharedPreferences pref = getActivity().getSharedPreferences("PackageType", Context.MODE_PRIVATE);

Related

How to clear shared preferences from fragment?

How i can clear shared preferences from fragment? Thank you
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
sharedPreferences = getActivity().getSharedPreferences("login.conf", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.clear();
editor.commit();
Intent logout = new Intent(getActivity(), LoginActivity.class);
startActivity(logout);
Log.d(TAG, sharedPreferences.getString("username", ""));
Log.d(TAG, sharedPreferences.getString("password", ""));
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_logout, container, false);
}
That my fragment
you can directly use preference name and clear it from anywhere.
SharedPreferences preferences = getSharedPreferences("Mypref", 0);
preferences.edit().remove("shared_pref_key").commit();
or
SharedPreferences preferences = context.getSharedPreferences("Mypref", Context.MODE_PRIVATE);
preferences .edit().clear().commit();
Use something like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment_logout, container, false);
SharedPreferences sharedPreferences = getActivity().getSharedPreferences("login.conf", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.commit();
Intent logout = new Intent(getActivity(), LoginActivity.class);
startActivity(logout);
Log.d(TAG, sharedPreferences.getString("username", ""));
Log.d(TAG, sharedPreferences.getString("password", ""));
// Inflate the layout for this fragment
return view;
}

sharedpreferences is not saving or loading my data Android

Here is my savaData and loadData method which is used to save and load data
public void saveData(){
SharedPreferences sharedPreferences= getActivity().getSharedPreferences("SubjectTitle", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
int i=0,n;
n=addArray.size();
for(Bunk b: addArray) {
editor.putString("Title"+i, b.getTitle());
editor.putInt("No_of_bunk"+i, b.getBunk_remain());
editor.putFloat("pre_of_att"+i, b.getPrecentageAtt());
i++;
}
editor.putInt("size_of_data",n);
editor.apply();
Toast.makeText(getActivity(),"data saved",Toast.LENGTH_SHORT).show();
}
public ArrayList<Bunk> loadData(){
SharedPreferences sharedPreferences= getActivity().getSharedPreferences("SubjectTitle", Context.MODE_PRIVATE);
int n=sharedPreferences.getInt("size_pf_data",Default);
String loadTitle;
int loadBunk;
float loadAtt;
for(int i=0;i<n;i++){
loadTitle=sharedPreferences.getString("Title"+i,DEFAULT);
loadBunk=sharedPreferences.getInt("No_of_bunk"+i,Default);
loadAtt=sharedPreferences.getFloat("pre_of_att"+i,def_ault);
addArray.add(new Bunk(loadTitle,loadBunk,loadAtt));
}
return addArray;
}
here is code which is used to load data from loadData method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup perent, Bundle savedInstanceState) {
super.onCreateView(inflater, perent, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_main, perent, false);
show=(ListView)v.findViewById(android.R.id.list);
addArray=loadData();
adapter=new BunkAdapter(addArray);
show.setAdapter(adapter);
return v;
}
First, use editor.commit(); to save data after puting them, or directly: editor.putString("Title"+i, b.getTitle()).commit();
Secondly, I believe you're thinking wrong, Shared Preferences is better for things like settings or small amounts of data, in your case, you should use SQLite to save large data.

How to get the status of each string in each page that is assigned to the fragment?

#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//Animation animation = AnimationUtils.loadAnimation(this.getContext(), R.anim.fade);
sectionlabeltextview = (TextView) rootView.findViewById(R.id.section_label);
sectionlabeltextview.setText(getArguments().getString("text"));
sectionlabeltextview.setTextColor(Color.BLACK);
sectionlabeltextview.setTextSize(25);
sectionlabel_text = (String) sectionlabeltextview.getText();
Toast.makeText(getContext(),sectionlabel_text,Toast.LENGTH_SHORT).show();
Log.e("sectionlabel_text",sectionlabel_text);
SharedPreferences sharedPref=getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPref.edit();
editor.putString("catch_text",sectionlabel_text);
editor.commit();
//sectionlabeltextview.startAnimation(animation);
//sectionlabeltextview.setGravity();
return rootView;
}
Here the values of sectionlabel_text and toast message are getting differed, though both are in the same function.Please try to give the solution?

Saving a cardview programmatically even after closing the activity

I am very new to android and I am trying to make CardView by programmatically, I am using Fragment. By setting an OnClick on the button I am able to create cards in my layout. The issue is after closing the activity cards are not showing.
How can I save the cards, so after closing the activity cards should be there?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle("Okhlee");
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.hawker_jobs_fragment_layout, container, false);
context = getContext();
button = (Button) view.findViewById(R.id.hawker_job_add_card_view_button);
button.setOnClickListener(this);
relativeLayout = (RelativeLayout) view.findViewById(R.id.hawker_job_relative_layout);
return view;
}
#Override
public void onClick(View v) {
createCardView();
}
/*Adding cardview programmatically function */
private void createCardView() {
CardView addedCardView = new CardView(context);
layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, height);
addedCardView.setLayoutParams(layoutParams);
addedCardView.setPadding(10 ,10, 10, 10);
addedCardView.setRadius(15);
addedCardView.setCardBackgroundColor(Color.BLUE);
addedCardView.setMaxCardElevation(30);
addedCardView.setMaxCardElevation(6);
relativeLayout.addView(addedCardView);
}
Sorry for my bad English.
You can use SharedPreferences in order to know whether you should display the card.
Here's a sample code using your original one -
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.hawker_jobs_fragment_layout, container, false);
context = getContext();
button = (Button) view.findViewById(R.id.hawker_job_add_card_view_button);
button.setOnClickListener(this);
relativeLayout = (RelativeLayout) view.findViewById(R.id.hawker_job_relative_layout);
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
boolean shouldDisplayCard = sharedPref.getBoolean("should_display_card", false);
if(shouldDisplayCard)
createCardView();
return view;
}
#Override
public void onClick(View v) {
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("should_display_card", true);
editor.apply();
createCardView();
}

How to create editText in fragment

I'd like to create editable textfield in fragment, which after close or stop of app would be saved. But there's something wrong in line with return notatki; I already have this:
public class DetailFragment2 extends Fragment {
private EditText notatki;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#SuppressLint("SimpleDateFormat")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details2, parent, false);
notatki = (EditText) view.findViewById(R.id.editText1);
SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
notatki.setText(settings.getString("value", ""));
return view;
}
#Override
public void onStop( ){
super.onStop();
if(notatki.getText() != null) {
SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("value", notatki.getText().toString());
editor.commit();
}
}
}
When I change return notatki; to return view; it works until the stop of app, when I wanted to save content of editText but it isn't saving anything.
Change to
private EditText notatki;
#SuppressLint("SimpleDateFormat")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details2, parent, false);
notatki = (EditText) view.findViewById(R.id.editText1);
SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
notatki.setText(settings.getString("value", ""));
return view; // return view here instead of notaki
}
You have already declared EditText as a class member
private EditText notatki;
So just initialize it in onCreateView.
Change
EditText notatki = (EditText) view.findViewById(R.id.editText1);
to
notatki = (EditText) view.findViewById(R.id.editText1);
because if you declare it again in your onCreateView it will shadow the global one and you will get it null in onStop
Try This....
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details2, parent, false);
EditText notatki = (EditText) view.findViewById(R.id.editText1);
SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
notatki.setText(settings.getString("value", ""));
return view;
}
You are doing it right but the problem is that you should return the parent view from onCreateView and not the EditText instance notatki
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details2, parent, false);
....
return view;
}
private EditText etext;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.secondfragment,container,false);
etext = (EditText) getView().findViewById(R.id.editText4);
return v;

Categories

Resources