Reputation: 49
So i created a navigationDrawer and i have a problem
I need to put my activity
ActivitySec
package com.calculatorvamal.app;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class SecondAct2 extends Activity implements TextWatcher {
private EditText Result;
private String Operation;
private EditText editYear;
private EditText editCapacity;
private Button calc;
private RadioButton radioD;
private RadioButton radioB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_home2);
TextView Year = (TextView) findViewById(R.id.Year);
TextView Capacity = (TextView) findViewById(R.id.Capacity);
editYear = (EditText) findViewById(R.id.editYear);
editYear.addTextChangedListener(this);
editCapacity = (EditText) findViewById(R.id.editCapacity);
final EditText Result = (EditText) findViewById(R.id.Result);
final Button calc = (Button) findViewById(R.id.calc);
final RadioButton radioD = (RadioButton) findViewById(R.id.radioD);
final RadioButton radioB = (RadioButton) findViewById(R.id.radioB);
TextWatcher listener = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (editYear.getText().toString().length() == 0 || editCapacity.getText().toString().length() == 0) {
calc.setEnabled(false);
} else {
calc.setEnabled(true);
}
}
};
editYear.addTextChangedListener(listener);
editCapacity.addTextChangedListener(listener);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int edityear_value = Integer.parseInt(editYear.getText().toString());//Convert to intger
double result = 0;
int NumberCapacity = Integer.parseInt(editCapacity.getText().toString());//Convert to intger
if (radioB.isChecked()) {
switch (edityear_value) {
case 2004:
Operation = "2004";
break;
case 2005:
Operation = "2005";
break;
case 2006:
Operation = "2006";
break;
case 2007:
Operation = "2007";
break;
case 2008:
Operation = "2007";
break;
case 2009:
Operation = "2009";
break;
case 2010:
Operation = "2009";
break;
case 2011:
Operation = "2011";
break;
case 2012:
Operation = "2011";
break;
case 2013:
Operation = "2013";
break;
case 2014:
Operation = "2013";
break;
default:
Operation = "error";
}
} else if (radioD.isChecked()) {
switch (edityear_value) {
case 2004:
Operation = "20041";
break;
case 2005:
Operation = "20051";
break;
case 2006:
Operation = "20061";
break;
case 2007:
Operation = "20071";
break;
case 2008:
Operation = "20071";
break;
case 2009:
Operation = "20091";
break;
case 2010:
Operation = "20091";
break;
case 2011:
Operation = "20111";
break;
case 2012:
Operation = "20111";
break;
case 2013:
Operation = "20131";
break;
case 2014:
Operation = "20131";
break;
default:
Operation = "error";
}
}
//Benzin
if (NumberCapacity > 0 && NumberCapacity <= 1000) {
if (Operation.equals("2004")) {
result = NumberCapacity * 0.52;
}
if (Operation.equals("2005")) {
result = NumberCapacity * 0.50;
}
if (Operation.equals("2006")) {
result = NumberCapacity * 0.47;
}
if (Operation.equals("2007")) {
result = NumberCapacity * 0.44;
}
if (Operation.equals("2009")) {
result = NumberCapacity * 0.43;
}
if (Operation.equals("2009")) {
result = NumberCapacity * 0.43;
}
if (Operation.equals("2011")) {
result = NumberCapacity * 0.42;
}
if (Operation.equals("2013")) {
result = NumberCapacity * 0.40;
}
}
if (NumberCapacity > 1001 && NumberCapacity <= 1500) {
if (Operation.equals("2004")) {
result = NumberCapacity * 0.67;
}
if (Operation.equals("2005")) {
result = NumberCapacity * 0.63;
}
if (Operation.equals("2006")) {
result = NumberCapacity * 0.60;
}
if (Operation.equals("2007")) {
result = NumberCapacity * 0.56;
}
if (Operation.equals("2009")) {
result = NumberCapacity * 0.55;
}
if (Operation.equals("2011")) {
result = NumberCapacity * 0.54;
}
if (Operation.equals("2013")) {
result = NumberCapacity * 0.52;
}
}
if (NumberCapacity >= 1501 && NumberCapacity <= 2000) {
if (Operation.equals("2004")) {
result = NumberCapacity * 1.00;
}
if (Operation.equals("2005")) {
result = NumberCapacity * 0.95;
}
if (Operation.equals("2006")) {
result = NumberCapacity * 0.89;
}
if (Operation.equals("2007")) {
result = NumberCapacity * 0.84;
}
if (Operation.equals("2009")) {
result = NumberCapacity * 0.83;
}
if (Operation.equals("2011")) {
result = NumberCapacity * 0.82;
}
if (Operation.equals("2013")) {
result = NumberCapacity * 0.80;
}
}
if (NumberCapacity >= 2001 && NumberCapacity <= 3000) {
if (Operation.equals("2004")) {
result = NumberCapacity * 1.59;
}
if (Operation.equals("2005")) {
result = NumberCapacity * 1.52;
}
if (Operation.equals("2006")) {
result = NumberCapacity * 1.44;
}
if (Operation.equals("2007")) {
result = NumberCapacity * 1.36;
}
if (Operation.equals("2009")) {
result = NumberCapacity * 1.35;
}
if (Operation.equals("2011")) {
result = NumberCapacity * 1.34;
}
if (Operation.equals("2013")) {
result = NumberCapacity * 1.32;
}
}
if (NumberCapacity >= 3000) {
if (Operation.equals("2004")) {
result = NumberCapacity * 3.57;
}
if (Operation.equals("2005")) {
result = NumberCapacity * 3.56;
}
if (Operation.equals("2006")) {
result = NumberCapacity * 3.55;
}
if (Operation.equals("2007")) {
result = NumberCapacity * 3.54;
}
if (Operation.equals("2009")) {
result = NumberCapacity * 3.53;
}
if (Operation.equals("2011")) {
result = NumberCapacity * 3.52;
}
if (Operation.equals("2013")) {
result = NumberCapacity * 3.50;
}
}
//Diesel
if (NumberCapacity <= 1500) {
if (Operation.equals("20041")) {
result = NumberCapacity * 0.67;
}
if (Operation.equals("20051")) {
result = NumberCapacity * 0.63;
}
if (Operation.equals("20061")) {
result = NumberCapacity * 0.60;
}
if (Operation.equals("20071")) {
result = NumberCapacity * 0.56;
}
if (Operation.equals("20091")) {
result = NumberCapacity * 0.55;
}
if (Operation.equals("20111")) {
result = NumberCapacity * 0.54;
}
if (Operation.equals("20131")) {
result = NumberCapacity * 0.52;
}
}
if (NumberCapacity >= 1501 && NumberCapacity <= 2500) {
if (Operation.equals("20041")) {
result = NumberCapacity * 1.59;
}
if (Operation.equals("20051")) {
result = NumberCapacity * 1.52;
}
if (Operation.equals("20061")) {
result = NumberCapacity * 1.44;
}
if (Operation.equals("20071")) {
result = NumberCapacity * 1.36;
}
if (Operation.equals("20091")) {
result = NumberCapacity * 1.35;
}
if (Operation.equals("20111")) {
result = NumberCapacity * 1.34;
}
if (Operation.equals("20131")) {
result = NumberCapacity * 1.32;
}
}
if (NumberCapacity >= 2501) {
if (Operation.equals("20041")) {
result = NumberCapacity * 3.57;
}
if (Operation.equals("20051")) {
result = NumberCapacity * 3.56;
}
if (Operation.equals("20061")) {
result = NumberCapacity * 3.55;
}
if (Operation.equals("20071")) {
result = NumberCapacity * 3.54;
}
if (Operation.equals("20091")) {
result = NumberCapacity * 3.53;
}
if (Operation.equals("20111")) {
result = NumberCapacity * 3.52;
}
if (Operation.equals("20131")) {
result = NumberCapacity * 3.50;
}
}
Result.setText(String.valueOf(result));//Set the value after calculate it
//Set the value after calculate it
}
}
);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
try {
int no = Integer.parseInt(s.toString());
if (no < 9) {
Toast.makeText(this, "Autovehicolul nu trebuie sa fie mai vechi de 10 ani", Toast.LENGTH_SHORT).show();
}
}
catch(NumberFormatException e){}
}
}
in my fragment clas
Fragment.class
package com.calculatorvamal.app;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HomeFragment extends Fragment {
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
}
i tried doing it another way with intent and start activity but it gives me a lag(30,40 milliseconds delay when starting) when activity is started. Fell free to give any suggestion,thank you.
Upvotes: 0
Views: 208
Reputation: 5012
I think that you have misunderstood or not read up on how to communicate between Fragments and Activities. You should read up on how to do so: https://developer.android.com/training/basics/fragments/communicating.html.
Also you should not mix your Fragment logic and your Activity logic. Actions to be performed with aspects or Views (such as button click listeners and list view creation etc) from your discreet Fragments and their layouts should be contained in the Fragment class. If you need to pass results from one to the other for various reasons then you can communicate between the separate entities like so:
Passing information from Activity to Fragment:
To send information to a fragment you need only define a method in the Fragment to receive it and call that from your Activity:
public class MyActivity extends Activity
{
private MyFragment myFragment;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_holder);
myFragment = new MyFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, myFragment).commit();
}
private void testFragmentCommunication(String message)
{
myFragment.recievedCommunication(message);
}
}
public class MyFragment extends Fragment
{
...
public boolean recievedCommunication(String message)
{
Log.d("MyFragment", "Recieved communication from Activity: " + message);
}
...
}
Passing information from Fragment to Activity:
However if you need to pass information back from your Fragment to your Activity you will need to define an Interface for them to use:
public class MyFragment extends Fragment
{
MyActivityInterface activityInterface;
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
activityInterface = (MyActivityInterface) activity;
}
catch (ClassCastException e)
{
Log.e(TAG, "Parent Activity deosn't implement 'MyActivityInterface'");
throw new ClassCastException(activity.toString()
+ " must implement MyActivityInterface");
}
}
public Interface MyAcyivityInterface
{
public void onMessageReceived();
}
private void testActivityCommunication()
{
activityInterface.onMessageReceived(String message);
}
}
public class MyActivity extends Activity implements MyFragment.MyActivityInterface
{
private MyFragment myFragment;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_holder);
myFragment = new MyFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
public void onMessageReceived(String message)
{
Log.d("MyActivity", "Recieved communication from Fragment: " + message);
}
}
Upvotes: 1