Why Android CursorAdapter call 3 times the same action - android

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.

Related

why is FindViewById not coming out? it turns red as a mistake if I write it myself [duplicate]

This question already has answers here:
findViewById in Fragment
(37 answers)
Closed 2 years ago.
Why does FindViewById not work? If I type findView ..., findViewById does not appear, and if I type it myself, it writes error. my code (fragment):
public class BiografiyaFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public BiografiyaFragment() {
}
public static BiografiyaFragment newInstance(String param1, String param2) {
BiografiyaFragment fragment = new BiografiyaFragment();
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) {
return inflater.inflate(R.layout.fragment_biografiya, container, false);
}
}
screenshot
It's a fragment. Therefore, you have to create a object of View class in onCreateView method. So, the code in onCreateView will be,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_biografiya, container, false);
// I am assuming a textview here....
TextView textView=view.findViewById(R.id.textView);
return view;
}
In the modern way, the best solution is to use ViewBinding.
Usage steps
build.gradle
android {
...
buildFeatures {
viewBinding true
}
}
your code
public class BiografiyaFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private BiografiyaBinding binding; // new added.
public BiografiyaFragment() {
}
public static BiografiyaFragment newInstance(String param1, String param2) {
BiografiyaFragment fragment = new BiografiyaFragment();
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) {
binding = BiografiyaBinding.inflate(inflater,container,false); // Added
return binding.getRoot();
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
// Sample
//binding.sampleViewIdName...
}
}
in the fragment, you have to call this function by associating with view
like
view.findViewById()
and here in you case, view is
View view = inflater.inflate(R.layout.fragment_biografiya, container, false);

How to start a new activity from Textview/Button of a Fragment inside a Tabbed Activity

As showing in the above picture, I have created a Fragment named CustomerFragLogin with the code as follows :-
public class customerFragLogin extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public customerFragLogin() {
// Required empty public constructor
}
public static customerFragLogin newInstance(String param1, String param2) {
customerFragLogin fragment = new customerFragLogin();
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) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.frag_customer_login, container, false);
}
}
Now, when I click on 'Login' button or 'Forget password' textview I want to open a new activity. How do I implement this ?
add this to your CustomerFragLogin
#Override
public void onViewCreated(#NotNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView textView= findViewById(R.id.textView)
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(getActivity(), NewActivity.class));
}
});
}
make sure your activity is declared in the manifest

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.

Android Studio Shared Preferences

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);
.............
....
}

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