Pass text to an Edittext

Asked

Viewed 719 times

-1

I’m making an android app that reads a barcode. I saw a video on Youtube that taught how to do and replicated to take as a basis and worked well. The problem was when I tried to put the read return on a EditText. In the video, the result appears with Toast. No matter how hard I try, there’s nothing on EditText.

Follow the Java code:

package none.teste;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;

import android.widget.Toast;

import com.google.zxing.Result;

import me.dm7.barcodescanner.zxing.ZXingScannerView;

import static none.teste.R.layout.activity_menu;

public class Menu extends AppCompatActivity implements ZXingScannerView.ResultHandler{

private ZXingScannerView leitorView;

    EditText editCodigo;

    Leitor l=new Leitor();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_menu);


        editCodigo = (EditText) findViewById(R.id.editCodigo);
        Toast.makeText(Menu.this,"onCreate",Toast.LENGTH_LONG).show();



    }
    public void LEITURA(View view) {

        leitorView = new ZXingScannerView(this);
        setContentView(leitorView);
        leitorView.setResultHandler(this);
        leitorView.startCamera();
        Toast.makeText(Menu.this,"LEITURA",Toast.LENGTH_LONG).show();


    }

    @Override
    protected void onPause() {
        super.onPause();
        leitorView.stopCamera();
        Toast.makeText(Menu.this,"onPause",Toast.LENGTH_LONG).show();
    }




    @Override
    public void handleResult(Result result) {

        leitorView.stopCamera();
        setContentView(R.layout.activity_menu);
        l.setLido(result.getText().toString());
        editCodigo.setText(l.getLido());
        Toast.makeText(Menu.this,"handleResult",Toast.LENGTH_LONG).show();
        leitorView.resumeCameraPreview(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Toast.makeText(Menu.this,"onResume",Toast.LENGTH_LONG).show();
    }

}

These Toasts I put to know where the application was going and the class Leitor It’s bullshit, it only has one attribute String with getter and Setter, you don’t need that. Anyway, I wanted to pass the result to the EditText.

  • Sometimes we’ll make silly mistakes. You made sure result.getText().toString() returns something?

  • In fact the solution may be exactly there, the text being informed in editCodigo.setText(l.getLido()); comes from result.getText().toString(). He said that the result appears in Toast, but in fact only this here: Toast.makeText(Menu.this,"handleResult",Toast.LENGTH_LONG).show();.Surely you should check if Result in the method handleResult is not coming empty.

  • Natan, I made sure yes, when I put -> Toast.makeText(Menu.this,result.gettext(),Toast.LENGTH_LONG). s how(); works well, but the problem is that it only works with Toast, when I try to put in Edit with setText this is blank, IE, has a return, but for some reason the business does not appear. Note that if I put setText with anything else, like editCodigo.setText("TEST"); TEST appears in the field. Thanks anyway. : ) I think the problem is in the return to activity_menu, but I’m not sure. I’ll see if I can fix it agr. If you know something says there

  • Glr, I solved my problem. The error was related to Activity msm, I have to tinker with the manifest and other things. Thank you all!!!

1 answer

0

editCodigo.setText("seu código de barras").

This allows your field that has already been declared in the class to receive the value that has been read.

  • 2

    Boy, I’m amazed at the current trend I’ve been getting here from people who want to program without knowing anything, not even the basics. Just copy and paste codes from some places and if it doesn’t work, why not come ask here how to do things...

  • I’ve tried using setText, but nothing happens with Edit, it goes blank, appearing in the case of Hint. I know this command works because I’ve used it in other apps, I even put it inside onCreate that runs when starting the app, to see what happened, and Dae appears the text in Edit, but I can’t put the result there and I don’t know why this is happening.

  • You are using Zxingscannerview(). When I answered the topic I got to watch the mentioned video and also do some research. In the article I found I saw some people mentioning that there is already a native implementation of google for barcode reading. Try to find something like this. The tutorial may be outdated.

Browser other questions tagged

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