Spdwiz18
Spdwiz18

Reputation: 31

passing a variable to classes

i can not figure out how to pass the results from onActivityResult to resultBreakdown. I know there is a get/set and I've looked at a few tutorials, but I'm just not getting it. Or, Is there a better way? The program runs fine up if i comment out */ the resultBreakdown Class

Side note, I just started with java/android. I'm a better learner at doing then reading. I know my code is a little clumsy. Thanks for the help

Note: i edited code to reflect suggested changes

package com.example.spdwiz18.testproject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;

import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.vision.barcode.Barcode;

import java.text.DateFormat;
import java.time.*;
import java.time.temporal.*;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;


public class GrindLogActivity extends AppCompatActivity {
TextView barcodeResult;




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

    // this is how to set id's from the xml file with vNames. (julianDate)
    TextView bcc = (Textview)findViewById(R.id.bccheck);
    TextView pc = (Textview)findViewById(R.id.pcode);
    TextView pd = (Textview)findViewById(R.id.pdate);
    // TextView en = (Textview)findViewById(R.id.estnum);
    TextView sn = (Textview)findViewById(R.id.seqnum);
    TextView nw = (Textview)findViewById(R.id.nweight);
    barcodeResult = (TextView) findViewById(R.id.barcode_result);
    TextView julianDate = (TextView) findViewById(R.id.datecode);
    TextView td1 = (TextView) findViewById(R.id.todaydate1);


    // this is how you get a julian/original date for android
        LocalDate now = LocalDate.now();
        int julian = now.get(ChronoField.DAY_OF_YEAR);
    // this how to set you current date for android
        Date date = new Date();
        String stringDate = DateFormat.getDateInstance().format(date);
    // this is how to set your vNames to your method variables
        julianDate.setText(Integer.toString(julian));
        td1.setText(stringDate);

}

/*add click event to the scan barcode button */
public void scanBarcode(View v) {
    Intent intent = new Intent(this, ScanBarcodeActivity.class);
    startActivityForResult(intent, 0);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 0) {
        if (requestCode == CommonStatusCodes.SUCCESS) {
            if (data != null) {
                Barcode barcode = data.getParcelableExtra("barcode");
                barcodeResult.setText("Barcode value : " + barcode.displayValue);
            } else {
                barcodeResult.setText("No Barcode Found");
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
public void resultsBreakdown(String result) {
if (result.length() == 44) {
    pc.setText(result.CharSequence(2,10));
    pd.setText(result.CharSequence(13,18));
    sn.setText(result.CharSequence(21,27));
    nw.setText(result.CharSequence(13,18));
} else {
    bcc.setText("invalid barcode");
}

}

Upvotes: 0

Views: 126

Answers (5)

Spdwiz18
Spdwiz18

Reputation: 31

Thank you for everyones help. This one worked as needed

package com.example.spdwiz18.testproject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;

import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.vision.barcode.Barcode;

import java.text.DateFormat;
import java.time.*;
import java.time.temporal.*;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;


public class GrindLogActivity extends AppCompatActivity {
TextView barcodeResult;
TextView bcc;
TextView pc;
TextView pd;
TextView sn;
TextView nw;


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


    bcc = (TextView)findViewById(R.id.bccheck);
    pc = (TextView)findViewById(R.id.pcode);
    pd = (TextView)findViewById(R.id.pDate);
    // TextView en = (TextView)findViewById(R.id.estnum);
    sn = (TextView)findViewById(R.id.seqnum);
    nw = (TextView)findViewById(R.id.nweight);
    barcodeResult = (TextView) findViewById(R.id.barcode_result);
    TextView julianDate = (TextView) findViewById(R.id.datecode);
    TextView td1 = (TextView) findViewById(R.id.todaydate1);

    // this is how to set id's from the xml file with vNames. (julianDate)

    // this is how you get a julian/original date for android
        LocalDate now = LocalDate.now();
        int julian = now.get(ChronoField.DAY_OF_YEAR);
    // this how to set you current date for android
        Date date = new Date();
        String stringDate = DateFormat.getDateInstance().format(date);
    // this is how to set your vNames to your method variables
        julianDate.setText(Integer.toString(julian));
        td1.setText(stringDate);

}

/*add click event to the scan barcode button */
public void scanBarcode(View v) {
    Intent intent = new Intent(this, ScanBarcodeActivity.class);
    startActivityForResult(intent, 0);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 0) {
        if (requestCode == CommonStatusCodes.SUCCESS) {
            if (data != null) {
                Barcode barcode = data.getParcelableExtra("barcode");
                barcodeResult.setText("Barcode value : " + barcode.displayValue);
                resultsBreakdown(barcode.displayValue.toString());
            } else {
                barcodeResult.setText("No Barcode Found");
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
public void resultsBreakdown(CharSequence result) {
    if (result.length() == 44) {
        pc.setText(result.subSequence(2,10));
        pd.setText(result.subSequence(13,18));
        sn.setText(result.subSequence(21,27));
        nw.setText(result.subSequence(13,18));
    } else {
        bcc.setText("invalid barcode");
    }

}}

Upvotes: 0

user2711811
user2711811

Reputation:

The code posted needs a lot of work - so this answer most likely is just partial

(1) TextView initialization in wrong place

The TextViews declared as class instance variables cannot also be initialized at that point. So leave the declarations there (where bardcodeResult is declared):

TextView barcodeResult;
TextView bcc;
TextView pc;
TextView pd;
TextView sn;
TextView nw;

but move the initialization to the onCreate method in the same manner barcodeResult.

// in onCreate
barcodeResult = (TextView) findViewById(R.id.barcode_result);
bcc = (TextView)findViewById(R.id.bccheck);
pc = (TextView)findViewById(R.id.pcode);
pd = (TextView)findViewById(R.id.pdate);
sn = (TextView)findViewById(R.id.seqnum);
nw = (TextView)findViewById(R.id.nweight);

(2) The syntax is of resultsBreakdown is invalid and the functionality is wrong (need to invoke setText)- try:

  public void resultsBreakdown(String result) {
    if (result.length() == 44) {
         pc.setText(result.CharSequence(2,10));
         pd.setText(result.CharSequence(13,18));
       sn.setText(result.CharSequence(21,27));
         nw.setText(result.CharSequence(13,18));
    } else {
        bcc.setText("invalid barcode");
    }
}

(3) Actually invoke the resultsBreakdown method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 0) {
        if (requestCode == CommonStatusCodes.SUCCESS) {
            if (data != null) {
                Barcode barcode = data.getParcelableExtra("barcode");
                barcodeResult.setText("Barcode value : " +                  barcode.displayValue);

                //-- THIS LINE WAS ADDED TO CALL METHOD
                resultsBreakdown(barcode.displayValue.toString());
           } else {
               barcodeResult.setText("No Barcode Found");
           }
           super.onActivityResult(requestCode, resultCode, data);
        }
   }
}

Upvotes: 1

Upendra Shah
Upendra Shah

Reputation: 2301

I think there should be resultCode instead of requestCode in requestCode == CommonStatusCodes.SUCCESS

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 0) {
        if (resultCode == Activity.RESULT_OK) {
            if (data != null) {
                Barcode barcode = data.getParcelableExtra("barcode");
                barcodeResult.setText("Barcode value : " + barcode.displayValue);
                resultsBreakdown(barcode.displayValue)
            } else {
                barcodeResult.setText("No Barcode Found");
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
public void resultsBreakdown(String barcodeData){
    if (barcodeData.length == (44)) {
        pc = (barcodeData.CharSequence(2,10);
        pd = (barcodeData.CharSequence(13,18);
        sn = (barcodeData.CharSequence(21,27);
        nw = (barcodeData.CharSequence(13,18);
    )else(
        bcc = "invalid barcode";
    }
)               
}

Upvotes: 1

user2886765
user2886765

Reputation:

The code is incomplete as you have not placed ScanBarcodeActivity java file. Well... I am placing a small code in my project which will clear your concept and even you can use it.


Here suppose i am in Activity MyClassA.java where is a button and when it is clicked it will start another activity MyClassB.java, Remember it will start that activity along with layout.. from that activity you have to finish it on certain event and when it is finished your MyClassA.java will collect its result...


From Activity MyClassA.java on button click i want to start a activity MyClassB.java for result:

ContactsBtn.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            ContactsBtn.setEnabled(false);
            Intent intent = new Intent(MyClassA.this, MyClassB.class);
            startActivityForResult(intent, REQUEST_CODE);

        }
    });

Where REQUEST_CODE should be declared in MyClassA class as public static final int REQUEST_CODE = 1;


Now From Activity MyClassB.java on particular event say on click of a button you want to close this activity and want to send result to previous activity :

SelectContactsButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent intent = new Intent();
            intent.putStringArrayListExtra("WhitelistNames", (ArrayList<String>) WhitelistNames);

            setResult(Activity.RESULT_OK, intent);
            finish();
          }
    });

Here i am placing WhitelistNames array list as result to be sent to calling activity.


Now From Activity MyClassA.java again you have to collect the result with :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{

    if (requestCode == REQUEST_CODE)
    {

        if (resultCode == Activity.RESULT_OK)
        {
            ArrayList<String> WhitelistNames = data.getStringArrayListExtra("WhitelistNames");

            Log.d("ContactsContracts", "\nPREVIOUS LIST  : "+ContactsNumbers);
        }
   }
}

Remember onActivityResult method should be part of your MyClassA.java class and should not be in onCreate nor in some other existing standard methods of this class.

Hope it helps

Upvotes: 0

anilkay
anilkay

Reputation: 144

resultBreakdown neither a class nor a method.İt isn't even compile. You change your resultBreakDown to Method:

public void resultsBreakdown(Barcode barcodeResult){
    if (barcodeResult.length == (44)) {
        pc = (barcodeResult.CharSequence(2,10);
        pd = (barcodeResult.CharSequence(13,18);
        sn = (barcodeResult.CharSequence(21,27);
        nw = (barcodeResult.CharSequence(13,18);
}       else{
                bcc = "invalid barcode";
    }

and later in onActivityResult call your method with Barcode parameters.

Upvotes: 0

Related Questions