ads

vendredi 18 septembre 2015

Unable to Send data from One Activity to Another Activity



**FIRSTACTIVITY**

Code:


public class FirstMarks extends Activity {

    TextView tv_Subjects1, tv_Internal1, tv_External1;
    EditText et_Int_Eng, et_Int_M1, et_Int_MM, et_Int_Phy, et_Int_Chem, et_Int_Cp, et_Int_ElcsLab, et_Int_EpLab, et_Int_ItLab, et_Int_Draw;
    EditText et_Ext_Eng, et_Ext_M1, et_Ext_MM, et_Ext_Phy, et_Ext_Chem, et_Ext_Cp, et_Ext_ElcsLab, et_Ext_EpLab, et_Ext_ItLab, et_Ext_Draw;
    Button btn_Cal1;
    int IEng, IM1, IMM, IPhy, IChem, ICp, IElcsLab, IItLab, IEpLab, IDraw;
    int EEng, EM1, EMM, EPhy, EChem, ECp, EElcsLab, EItLab, EEpLab, EDraw;
    int ITotal1 =0, ETotal1 =0, Total1 =0;
    float Percentage1=0;
    String STotal1=" ", SPercentage1=" ";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.marks_1);

        //SETTING FONT.
        Typeface mytypeface = Typeface.createFromAsset(getAssets(), "font.ttf");

        //INITIALISING VIEWS.
        //INITIALISING TEXT VIEWS.
        tv_Subjects1 = (TextView) findViewById(R.id.tv_Subject1);
        tv_Internal1 = (TextView) findViewById(R.id.tv_Internal1);
        tv_External1 = (TextView) findViewById(R.id.tv_External1);
        //INITIALISING EDIT TEXT.
        //INTERNAL
        et_Int_Eng = (EditText) findViewById(R.id.et_Int_Eng);
        et_Int_M1 = (EditText) findViewById(R.id.et_Int_M1);
        et_Int_MM = (EditText) findViewById(R.id.et_Int_MM);
        et_Int_Phy = (EditText) findViewById(R.id.et_Int_Phy);
        et_Int_Chem = (EditText) findViewById(R.id.et_Int_Chem);
        et_Int_Cp = (EditText) findViewById(R.id.et_Int_Cp);
        et_Int_ElcsLab = (EditText) findViewById(R.id.et_Int_ElcsLab);
        et_Int_EpLab = (EditText) findViewById(R.id.et_Int_EpLab);
        et_Int_ItLab = (EditText) findViewById(R.id.et_Int_ItLab);
        et_Int_Draw = (EditText) findViewById(R.id.et_Ext_Draw);
        //EXTERNAL
        et_Ext_Eng = (EditText) findViewById(R.id.et_Ext_Eng);
        et_Ext_M1 = (EditText) findViewById(R.id.et_Ext_M1);
        et_Ext_MM = (EditText) findViewById(R.id.et_Ext_MM);
        et_Ext_Phy = (EditText) findViewById(R.id.et_Ext_Phy);
        et_Ext_Chem = (EditText) findViewById(R.id.et_Ext_Chem);
        et_Ext_Cp = (EditText) findViewById(R.id.et_Ext_Cp);
        et_Ext_ElcsLab = (EditText) findViewById(R.id.et_Ext_ElcsLab);
        et_Ext_EpLab = (EditText) findViewById(R.id.et_Ext_EpLab);
        et_Ext_ItLab = (EditText) findViewById(R.id.et_Ext_ItLab);
        et_Ext_Draw = (EditText) findViewById(R.id.et_Ext_Draw);
        //INITIALISING BUTTON
        btn_Cal1 = (Button) findViewById(R.id.btn_Cal1);

        //INITIALISING FONT.
        tv_Subjects1.setTypeface(mytypeface);
        tv_Internal1.setTypeface(mytypeface);
        tv_External1.setTypeface(mytypeface);
        btn_Cal1.setTypeface(mytypeface);

        //GETTING VALUES FROM EDIT TEXT.
        //ENGLISH.
        try {
            IEng = getInt(et_Int_Eng);
            EEng = getInt(et_Ext_Eng);

        } catch (NumberFormatException e) {
            Log.i("Log", "Not a ENGLISH Number");
        }

        //M1.
        try {
            IM1 = getInt(et_Int_M1);
            EM1 = getInt(et_Ext_M1);

        } catch (NumberFormatException e) {
            Log.i("Log", "Not a M1 Number");
        }
        //MM.
        try {
            IMM = getInt(et_Int_MM);
            EMM = getInt(et_Ext_MM);

        } catch (NumberFormatException e) {
            Log.i("Log", "Not a MM Number");
        }

        //PHYSICS.
        try {
            IPhy = getInt(et_Int_Phy);
            EPhy = getInt(et_Ext_Phy);

        } catch (NumberFormatException e) {
            Log.i("Log", "Not a PHYSICS Number");
        }

        //CHEMISTRY
        try {
            IChem = getInt(et_Int_Chem);
            EChem = getInt(et_Ext_Chem);

        } catch (NumberFormatException e) {
            Log.i("Log", "Not a CHEMISTRY Number");
        }

        //CP.
        try {
            ICp = getInt(et_Int_Cp);
            ECp = getInt(et_Ext_Cp);

        } catch (NumberFormatException e) {
            Log.i("Log", "Not a CP Number");
        }

        //DRAWING.
        try {
            IDraw = getInt(et_Int_Draw);
            EDraw = getInt(et_Ext_Draw);
        } catch (NumberFormatException e) {
            Log.i("Log", "Not a DRAW Number");
        }

        //ELCS LAB.
        try{
            IElcsLab = getInt(et_Int_ElcsLab);
            EElcsLab = getInt(et_Ext_ElcsLab);

        }catch (NumberFormatException e){
            Log.i("Log", "Not a ELCS Number");
        }

        //EP/EC LAB.
        try{
            IEpLab = getInt(et_Int_EpLab);
            EEpLab= getInt(et_Ext_EpLab);

        }catch (NumberFormatException e){
            Log.i("Log", "Not a EP LAB Number");
        }

        //IT LAB.
        try{
            IItLab = getInt(et_Int_ItLab);
            EItLab = getInt(et_Ext_ItLab);

        }catch (NumberFormatException e){
            Log.i("Log", "Not a ITLAB Number");
        }



        //TOTALING ALL SUBJECTS
        //TOTALING INTERNAL MARKS.
        ITotal1 = (IEng + IM1 + IMM + IPhy + IChem + ICp + IElcsLab + IEpLab + IItLab + IDraw);

        //TOTALING EXTERNAL MARKS.
        ETotal1 = (EEng + EM1 + EMM + EPhy + EChem + ECp + EElcsLab + EEpLab + EItLab + EDraw);

        //TOTALING INTERNAL & EXTERNAL MARKS.
        Total1 = (ITotal1 + ETotal1);

        //CONVERTING INT TOTAL1 INTO STRING.
        //STotal1 = String.valueOf(Total1);

        //PERCENTAGE CALCULATION.
        Percentage1 = (float) Total1 / 10;
        //SPercentage1 = String.valueOf(Percentage1);

        //WHEN BUTTON CALCULATE IS CLICKED.
        btn_Cal1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(FirstMarks.this, FirstResult.class);
                Bundle bundle = new Bundle();
                bundle.putInt("res1", Total1);
                bundle.putFloat("per1", Percentage1);
                i.putExtras(bundle);
                startActivity(i);
                finish();

            }
        });


    }

    //FUNCTION TO GET TEXT FROM EDIT TEXT AND CONVERT INTO INTEGER.
    private int getInt(EditText et) throws NumberFormatException {
        return Integer.valueOf(et.getText().toString());
    }
}


**SECOND ACTIVITY**

Code:


public class FirstResult extends Activity {

    EditText et_Total1,et_Percentage1;
    Button btn_Exit1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result_first);

        //INITIALISING VIEWS.
        et_Total1 = (EditText) findViewById(R.id.et_Total1);
        et_Percentage1 = (EditText) findViewById(R.id.et_Percentage1);
        btn_Exit1 = (Button) findViewById(R.id.btn_Exit1);

        //GETTING DATA FROM PREVIOUS ACTIVITY.
        Bundle b = getIntent().getExtras();
        int res1 = b.getInt("res1", 0);
        float per1 = b.getFloat("per1", 0);

      //CONVERTING TO STRING.
        String sres1 = String.valueOf(res1);
        String sper1 = String.valueOf(per1);

        //SETTING RESULTS.
        et_Total1.setText(sres1);
        et_Percentage1.setText(sper1);

        //WHEN EXIT IS CLICKED.
        btn_Exit1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                System.exit(0);
          }
        });


    }
}





Aucun commentaire:

Enregistrer un commentaire