Android Studio Shared Preferences - android

Thanks a lot for looking here, I'm working on an app for school and I've run into another problem. The main idea of the app is to track your calories and I would like it to save the calories so when the app is closed it will still remember them. I've been busy for a while now trying it with SavedPreferences but I keep getting errors. I was hoping someone could help me with this.
public class CaloriesEaten extends Fragment {
private Context mContext;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private static EditText caleaten;
private static EditText calalready;
private static TextView caltotal;
private static Button btnClick;
private EditText editText;
//private String calalreadystring = calalready.getText().toString();
//int CalCount = Integer.parseInt(calalreadystring);
Context context = getActivity();
SharedPreferences pref = context.getSharedPreferences("MyCalories", Context.MODE_PRIVATE);
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public CaloriesEaten() {
// Required empty public constructor
}
public static CaloriesEaten newInstance(String param1, String param2) {
CaloriesEaten fragment = new CaloriesEaten();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
String keki = pref.getString("CalorieCounter", "");
editText.setText(keki);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_calories_eaten,
container, false);
caleaten = (EditText) view.findViewById(R.id.CalorieInput);
calalready = (EditText) view.findViewById(R.id.Cals);
caltotal = (TextView) view.findViewById(R.id.CalNumber);
btnClick = (Button)view.findViewById(R.id.addcalories);
btnClick.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonClicked(v);
}
});
return view;
}
public void buttonClicked (View view) {
int x = Integer.parseInt(caleaten.getText().toString());
int y = Integer.parseInt(calalready.getText().toString());
int total = x + y;
caltotal.setText(Integer.toString(total));
calalready.setText(Integer.toString(total));
caleaten.setText("");
SharedPreferences.Editor edit = pref.edit();
String calalreadystring = calalready.getText().toString();
int CalCount = Integer.parseInt(calalreadystring);
edit.putInt("CalorieCounter", CalCount);
edit.commit();
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
I'm probably doing a lot of obvious dumb stuff but I really can't figure it out.
Thanks a lot!

declare SharedPreferences globally and initialize inside onCreateView methode,
eg:
Context context;
SharedPreferences pref;
.......
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_calories_eaten,
container, false);
context = getActivity();
pref = context.getSharedPreferences("MyCalories", Context.MODE_PRIVATE);
.............
....
}

Related

Why Android CursorAdapter call 3 times the same action

I have a problem with my CursorAdapter, I have to fill a listView with dynamic images.
I have a BBDD with some fields with value "1" or "0".
In bindView, when the fields returns 1 I add the image to a linearLayout.
The problem is that the image is added 3 times, and I don't know why.
girlsFragmentAdapter:
public class girlsListFragmentCursoAdapter extends CursorAdapter {
public girlsListFragmentCursoAdapter(Context context, Cursor c) {
super(context, c);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.girls_list_row,parent,false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView girlName;
ImageView imgExercice = new ImageView(context);
LinearLayout linearImgRow = (LinearLayout) view.findViewById(R.id.exercices_list_row);
girlName = view.findViewById(R.id.txtvwGirlName);
if (cursor.getString(cursor.getColumnIndexOrThrow("pull_ups")).equals("1") ) {
imgExercice.setImageResource(R.drawable.push_ups);
linearImgRow.addView(imgExercice);
Toast.makeText(context,"Pull ups: " +cursor.getString(cursor.getColumnIndexOrThrow("pull_ups")),Toast.LENGTH_SHORT ).show();
}
girlName.setText(cursor.getString(cursor.getColumnIndexOrThrow("nombre")));
}
}
I call this adapter from a Fragment ** when **OnActivityCreated is called
girls_fragment
public class girls_fragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private WodDbAdapter mWodAdapter;
ListView lvWods;
private View rootview;
public girls_fragment() {
// Required empty public constructor
}
public static girls_fragment newInstance(String param1, String param2) {
girls_fragment fragment = new girls_fragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lvWods = (ListView) getView().findViewById(R.id.lvwGirlsList);
mWodAdapter = new WodDbAdapter(getContext());
mWodAdapter.open();
fillData();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootview = inflater.inflate(R.layout.fragment_girls_fragment,container,false);
return rootview;
}
private void fillData(){
Cursor mGirlsCursor = mWodAdapter.fetchAllWods("girl");
getActivity().startManagingCursor(mGirlsCursor);
String [] from = new String[]{WodDbAdapter.KEY_NOMBRE,};
int [] to = new int []{R.id.txtvwGirlName};
girlsListFragmentCursoAdapter mGirlsAdapter = new girlsListFragmentCursoAdapter(rootview.getContext(),mGirlsCursor);
lvWods.setAdapter(mGirlsAdapter);
}
}
The result is this:
As you can see the image appears three times instead of one
It's because the following code:
if (cursor.getString(cursor.getColumnIndexOrThrow("pull_ups")).equals("1") ) {
imgExercice.setImageResource(R.drawable.push_ups);
linearImgRow.addView(imgExercice);
Toast.makeText(context,"Pull ups: " +cursor.getString(cursor.getColumnIndexOrThrow("pull_ups")),Toast.LENGTH_SHORT ).show();
}
with the following line:
linearImgRow.addView(imgExercice);
where you're always adding the imgExercice to the linearImgRow each times the bindView is called.

OnCickListener not working on EditText

On click listener not working on edit text in android
<EditText
android:id="#+id/state_search_UPFET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15sp"
android:clickable="true"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_below="#id/textView1"
android:hint="#string/hintState"
android:inputType="none"
android:padding="12sp" />
public class DoctorSearchFragment extends BaseFragment implements AbstractView, View.OnClickListener {
// TODO: Rename parameter arguments, choose names that match
private EditText SEARCH_STATE, SEARCH_CITY, DEPARTMENT;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
public static DoctorSearchFragment doctorSearchFragment;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
private Button getDoctorButton;
public DoctorSearchFragment() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static DoctorSearchFragment newInstance(String param1, String param2) {
DoctorSearchFragment fragment = new DoctorSearchFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doctorSearchFragment=this;
setHasOptionsMenu(false);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
AppController.getInstance().getMainActivity().getSupportActionBar().setTitle(getString(R.string.search_doctor_by));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_book_appointment, container, false);
initializeComponents(view);
return view;
}
private void initializeComponents(View view) {
SEARCH_CITY = (EditText) view.findViewById(R.id.city_search_UPFET);
SEARCH_STATE = (EditText) view.findViewById(R.id.state_search_UPFET);
DEPARTMENT = (EditText) view.findViewById(R.id.dept_search_UPFET);
getDoctorButton = (Button) view.findViewById(R.id.getDoctor) ;
SEARCH_STATE.setOnClickListener(this);
SEARCH_CITY.setOnClickListener(this);
DEPARTMENT.setOnClickListener(this);
getDoctorButton.setOnClickListener(this);
//progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.state_search_UPFET:
SEARCH_STATE.setError(null);
SEARCH_STATE.setText("");
StateListDialog stateListDialog = new StateListDialog();
stateListDialog.setAppCompatActivity((AppCompatActivity) getActivity());
stateListDialog.loadData();
break;
case R.id.city_search_UPFET:
SEARCH_CITY.setError(null);
SEARCH_CITY.setText("");
CityListDialog cityListDialog = new CityListDialog();
cityListDialog.setAppCompatActivity((AppCompatActivity) getActivity());
cityListDialog.loadData(SEARCH_STATE.getText().toString());
break;
case R.id.getDoctor:
AppController.getInstance().handleEvent(AppDefines.EVENT_ID_DOCTORSLIST);
break;
}
}
public void setState(String state) {
Log.d("TAG", "FRAGMENT STATE " + state + " IS STATE NULL " + (SEARCH_STATE == null));
if (SEARCH_STATE != null) SEARCH_STATE.setText(state);
if (SEARCH_CITY != null) SEARCH_CITY.setText("");
}
public void setCity(String city) {
Log.d("TAG", "FRAGMENT STATE " + city + " IS STATE NULL " + (SEARCH_CITY == null));
if (SEARCH_CITY != null) SEARCH_CITY.setText(city);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
#Override
public void update() {
}
#Override
public void onFragmentResume() {
AppController.getInstance().getMainActivity().getSupportActionBar().setTitle(getString(R.string.search_doctor_by));
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
public static DoctorSearchFragment getInstance() {
return doctorSearchFragment;
}
}

GridLayoutManager scrollToPosition doesn't scroll

I am passing the position of the RecyclerView from first FirstFragment to SecondFragment and it works. After scrolling in that one, I get the position again and I am passing it to the FirstFragment. But, when FirstFragment gets the position, the list is not scrolled to that particular index.
FirstFragment
public class FirstFragment extends BaseFragment implements FirstAdapter.OnItemInteractionListener {
FragmentFirstBinding mBinder;
List<MyItem> mMyItemList;
GridLayoutManager mManager;
MyItem mMyItem;
FirstAdapter mAdapter;
int mPosition;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
public static final String GET_SECOND_FRAGMENT = "get_second_fragment";
public static FirstFragment newInstance() {
FirstFragment fragment = new FirstFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMyItemList = new ArrayList<>();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
mBinder = DataBindingUtil.inflate(inflater, R.layout.fragment_first, container, false);
populate();
mManager = new GridLayoutManager(getContext(), 3, GridLayoutManager.VERTICAL, false);
mAdapter = new FirstAdapter(getContext(), mMyItemList);
mBinder.rvFirst.setLayoutManager(mManager);
mBinder.rvFirst.setAdapter(mAdapter);
mAdapter.addOnItemInteractionListener(this);
EventBus.getDefault().register(this);
return mBinder.getRoot();
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
#Override
public void onDestroyView() {
super.onDestroyView();
EventBus.getDefault().unregister(this);
}
private void populate() {
for (int i = 0; i < 30; i++) {
mMyItemList.add(i, mMyItem);
}
}
private void sendAction(String action, List<MyItem> myItemList, int position) {
if (mListener == null) {
return;
}
Bundle bundle = new Bundle();
bundle.putString(Constants.ACTION, action);
bundle.putParcelable(Constants.DATA1, Parcels.wrap(myItemList));
bundle.putInt(Constants.POSITION, position);
mListener.onFragmentInteraction(bundle);
}
#Override
public void onItemClick(int position) {
sendAction(GET_SECOND_FRAGMENT, mMyItemList, position);
}
#Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void onMessage(SendPos event) {
mPosition = event.getPos();
mBinder.rvFirst.scrollToPosition(mPosition);
mBinder.rvFirst.getLayoutManager().scrollToPosition(mPosition);
EventBus.getDefault().removeStickyEvent(event);
}
}
Second Fragment
public class SecondFragment extends BaseFragment {
FragmentSecondBinding mBinder;
List<MyItem> mMyItemList;
LinearLayoutManager mManager;
Parcelable mParcelable;
SecondAdapter mAdapter;
int mPosition;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
public static final String ACTION_BACK = "back";
public static SecondFragment newInstance(List<MyItem> myItemList, int position) {
SecondFragment fragment = new SecondFragment();
Bundle args = new Bundle();
args.putParcelable(ARG_PARAM1, Parcels.wrap(myItemList));
args.putInt(ARG_PARAM2, position);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParcelable = getArguments().getParcelable(ARG_PARAM1);
mMyItemList = Parcels.unwrap(mParcelable);
mPosition = getArguments().getInt(ARG_PARAM2);
}
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
mBinder = DataBindingUtil.inflate(inflater, R.layout.fragment_second, container, false);
mManager = new LinearLayoutManager(getContext());
mAdapter = new SecondAdapter(getContext(), mMyItemList);
mBinder.rvSecond.setLayoutManager(mManager);
mBinder.rvSecond.setAdapter(mAdapter);
mManager.scrollToPosition(mPosition);
setUIListeners();
return mBinder.getRoot();
}
private void setUIListeners() {
mBinder.back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPosition = mManager.findFirstVisibleItemPosition();
sendPos(mPosition);
sendActionToActivity(ACTION_BACK);
}
});
}
private void sendActionToActivity(String action) {
Bundle bundle = new Bundle();
bundle.putString(Constants.ACTION, action);
mListener.onFragmentInteraction(bundle);
}
private void sendPos(int pos) {
SendPos sendPos = new SendPos();
sendPos.setPos(pos);
EventBus.getDefault().postSticky(sendPos);
}
}
Have you try to use the RecyclerView as well ? for example :
mRecyclerView.scrollToPosition(mPosition);
mRecyclerView.getLayoutManager().scrollToPosition(mPosition);
Don't forget to register the EventBus : EventBus.getDefault().register(this);
EDIT:
You can use RecyclerView.smoothScrollToPosition(int position) instead
Hope this helps.
Sorry for my english.

Butter Knife #bind in Fragment error

I follow the instructions on the website : http://jakewharton.github.io/butterknife/
error : java.lang.RuntimeException: Unable to start activity ComponentInfo. java.lang.RuntimeException: Unable to bind views for com.project.myapp.OneFragment.
I try to remove #Bind(R.id.btnNext) Button btnNext; and run no error.
public class OneFragment extends Fragment {
#Bind(R.id.btnNext) Button btnNext;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public OneFragment() {
// Required empty public constructor
}
public static OneFragment newInstance(String param1, String param2) {
OneFragment fragment = new OneFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
ButterKnife.bind(this, view);
return view;
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
Replace this:
ButterKnife.bind(this, view);
with
ButterKnife.bind(getActivity(), view);

Buttons with fragments

I am working on an app for my school project in android studio that works with a navigation drawer and multiple fragments, I have got all the fragments setup and working but now I am stuck. I have no clue how to make working buttons in fragments.
Fragments activity
public class CaloriesEaten extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private static EditText caleaten;
private static EditText calalready;
private static TextView caltotal;
private static Button btnadd;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public CaloriesEaten() {
// Required empty public constructor
}
public static CaloriesEaten newInstance(String param1, String param2) {
CaloriesEaten fragment = new CaloriesEaten();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_calories_eaten,
container, false);
caleaten = (EditText) view.findViewById(R.id.CalorieInput);
calalready = (EditText) view.findViewById(R.id.Cals);
caltotal = (TextView) view.findViewById(R.id.CalNumber);
btnadd = (Button) view.findViewById(R.id.addcalories);
// Inflate the layout for this fragment
btnadd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonClicked(v);
}
});
return inflater.inflate(R.layout.fragment_calories_eaten, container, false);
}
public void buttonClicked (View view) {
int x = Integer.parseInt(caleaten.getText().toString());
int y = Integer.parseInt(calalready.getText().toString());
int total = x + y;
caltotal.setText(Integer.toString(total));
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
This is my first time asking a question here so if i'm missing anything please let me know.
Thanks!
View override by inflating at end of onCreateView() method
Replace
return inflater.inflate(R.layout.fragment_calories_eaten, container, false);
to
return view;
in onCreateView() method
Funny reason: You are inflating a View, using it to get it's children and finally returning different inflated view instance. You should return the same instance you are inflating initially and using to get it's children.
Just change
return inflater.inflate(R.layout.fragment_calories_eaten, container, false);
to
return view;
in onCreateView() method
Replace
return inflater.inflate(R.layout.fragment_calories_eaten, container, false);
to
view = inflater.inflate(R.layout.fragment_calories_eaten, container, false);
return view;
Now using this view instance you can find you other views inflating on it as button.
btnClickMe = (Button)view.findViewById(R.id.btn);
use above code in onCreateView method before returning view.

Categories

Resources