How do I recover a result from within the "onActivityResult" in my Activity

Asked

Viewed 177 times

0

I’m trying to use the camera phone as a barcode reader.

I can retrieve the barcode, and see the result inside the "onActivityResult", however, I need to take the result I got, to my main function, and I’m not getting.

Follow the code below.

Main function:

public void clickProducts(){


    listProdutos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            showCamera();

ShowCamera function:

private void showCamera() {
    try {
        //start the scanning activity from the com.google.zxing.client.android.SCAN intent
        Intent intent = new Intent(ACTION_SCAN);
        intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
        startActivityForResult(intent, 0);



    } catch (ActivityNotFoundException anfe) {
        //on catch, show the download dialog
        showDialog(ProductsListToThatSchoolAndUser.this, "Nenhuma câmera habilitada.", "Download a scanner code activity?", "Sim", "Não").show();
    }
}

onActivityResult:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            //get the extras that are returned from the intent
            String contents = intent.getStringExtra("SCAN_RESULT");

            Toast.makeText(ProductsListToThatSchoolAndUser.this, contents, Toast.LENGTH_LONG).show();


        }
    }
}

I would like to use the result of "Contents", within my function "clickProducts()", but I’m not getting it.

1 answer

1

I would like to use the result of "Contents", within my function "clickProducts()", but I’m not getting it.

You won’t get.

When the button is already clicked the method clickProducts() will have finished.
On the other hand, obtaining the result is done asynchronously. After showCamera() be called the method onItemClick()ends.

You will need to put the code that will handle the result in the method onActivityResult() or, better yet, create your own method and call it onActivityResult().

private void handleScanResult(String contents){
    //Faça aqui o que quer com `contents`
    Toast.makeText(ProductsListToThatSchoolAndUser.this, contents, Toast.LENGTH_LONG).show(); 
}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            //get the extras that are returned from the intent
            String contents = intent.getStringExtra("SCAN_RESULT");
            handleScanResult(contents);   
        }
    }
}
  • got it. I tried to set the content in an Edittext that I have inside this page and it didn’t work. I’m starting now android programming, many things are still new to me.

  • private void handleScanResult(String Contents){ //Do here what you want with contents&#xA; LayoutInflater inflater = getLayoutInflater();&#xA; final View dialogView = inflater.inflate(R.layout.alert_dialog_product ,null);&#xA; final EditText cbarrasLeitor = (EditText) dialogView.findViewById(R.id.cbarrascheck);&#xA;&#xA; cbarrasLeitor.setText(Contents); }

  • Bruno, this is another problem and should be addressed in another question. See how the site works on tour and in How to ask.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.