I know it's probably very simple, but my app is not working. I need to input weight in Edittext
and use its value, and the total distance value from the method CalculateDistance
and the value of the CalculateCalories
method.
With this code, I'm not able to click on Start
Button.
Where do I have to add code which gets the value from Edittext
and how can I make it work?
My code currently looks like this:
public class RunningModeFragment extends Fragment implements IGPSActivity
{
MyLocation myLocation=null;
Button btnRunningModeStart=null;
EditText txtWeight=null;
TextView txtTotalDistance=null;
TextView txtCalories=null;
Button btnShowRunningHistory = null;
String weight;
Float weightInKg;
Float caloriesBurned;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.running_mode_fragment, container, false);
}
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
txtTotalDistance=(TextView) getView().findViewById(R.id.txtTotalDistance);
txtWeight=(EditText) getView().findViewById(R.id.txtWeight);
txtCalories=(TextView) getView().findViewById(R.id.txtCalories);
if (txtWeight.getText().toString().isEmpty())
{
return;
}
else {
weight = txtWeight.getText().toString();
weightInKg = Float.parseFloat(weight);
}
btnRunningModeStart = (Button) getView().findViewById(R.id.btnRunningModeStart);
btnRunningModeStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
onClick_StartRunningMode(v);
}
});
btnShowRunningHistory = (Button) getView().findViewById(R.id.btnShowRunningHistory);
btnShowRunningHistory.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onClick_ShowRunningHistory(v);
}
});
}
private void onClick_ShowRunningHistory(View v) {
Fragment fragment = new RunningHistoryFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment, "Running History");
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();
}
private void onClick_StartRunningMode(View v)
{
if(myLocation ==null)
{
new Thread(new Runnable() {
public void run() {
DatabaseFacade dbfacade = new DatabaseFacade(getView().getContext());
Activity activity = new Activity(ActivityMode.RUNNING);
activity.setActivityId(activity.getActivityId());
activity.setStart(new Timestamp(new Date().getTime()));
dbfacade.saveActivity(activity);
}
}).start();
Toast.makeText(this.getActivity(), "Start Running mode", Toast.LENGTH_SHORT).show();
myLocation = new MyLocation();
myLocation.LocationStart(this);
btnRunningModeStart.setText("Stop");
}
else
{
Toast.makeText(this.getActivity(), "Stop Running mode", Toast.LENGTH_SHORT).show();
myLocation.LocationListenerStop();
myLocation = null;
btnRunningModeStart.setText("Start");
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
}
Location lastPoint = null;
float totalDistance = 0;
public static float CalculateDistance(Location startPoint, Location endPoint)
{
return (startPoint.distanceTo(endPoint) / ((float)1000));
}
public static float CalculateCalories(float a, float b){
return ((float)1.036)*a*b;
}
@Override
public void locationChanged(Location location)
{
try
{
if(lastPoint==null)lastPoint = location;
totalDistance += CalculateDistance(lastPoint, location);
lastPoint=location;
txtTotalDistance.setText(totalDistance + " km");
caloriesBurned=CalculateCalories(totalDistance,weightInKg);
txtCalories.setText(caloriesBurned + " kcal");
DatabaseFacade dbf = new DatabaseFacade(getView().getContext());
Activity activity = new Activity(ActivityMode.RUNNING);
dbf.saveLocation(new GpsLocation(activity.getActivityId(), location.getLongitude(), location.getLatitude(), location.getAccuracy()));
}catch (Exception E)
{
E.printStackTrace();
}
}
public void onResume(){
super.onResume();
((Main) getActivity()).setActionBarTitle("Running mode");
NavigationView navigationView = (NavigationView) getActivity().findViewById(R.id.nav_view);
// navigationView.setNavigationItemSelectedListener((Main) getActivity());
navigationView.setCheckedItem(R.id.runningMode);
}
}
I need to do the following: get the value from EditText txtWeight, parse it to float and use in CalculateCalories method
public static float CalculateCalories(float a, float b){
return ((float)1.036)*a*b;
}
I tried with this, but it's not working
if (txtWeight.getText().toString().isEmpty())
{
return;
}
else {
weight = txtWeight.getText().toString();
weightInKg = Float.parseFloat(weight);
}