0

I want to make and API to print tickets on zebra printers, I have my main activity(just for testing):

public class ZebraPrinterActivity extends Activity {
public EditText macAddress;
public Button testButton;
public Printer zebra;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    macAddress = (EditText) this.findViewById(R.id.editText1);
    testButton = (Button) this.findViewById(R.id.button1);

    zebra = new Printer(new ZebraPrinterActivity());
    testButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            new Thread(new Runnable() {
                public void run() {

                }
            }).start();
        }
    });
}}

and I have my class (API) called Printer:

public class Printer {

private ZebraPrinterConnection zebraPrinterConnection;
private ZebraPrinter zebra;
private String MAC;

public Printer (Activity activity) {
    zebraPrinterConnection = null;
    zebra = null;
    activity....get Edit Text
    this.MAC = MAC;
}}

What i need is getting the edit text from the early activity, is there any way to do this?

HXCaine
  • 4,228
  • 3
  • 31
  • 36
Fernando Santiago
  • 2,128
  • 10
  • 44
  • 75

1 Answers1

3

Have you tried this?

EditText macAddress = (EditText) activity.findViewById(R.id.editText1);
String macAddressString = macAddress.getText();

Some operations on Views such as EditText may not work due to a restriction on accessing UI elements in a non-UI thread. If that occurs, see this SO question for ways to access the EditText element from another thread:

Do some Android UI stuff in non-UI thread

Or the Android blog post on the same topic:

Painless Threading | Android Developers Blog

Community
  • 1
  • 1
HXCaine
  • 4,228
  • 3
  • 31
  • 36
  • You should be able to do that in the Printer class (see the 'activity' variable rather than 'this') – HXCaine Jun 08 '12 at 17:46
  • ok, you are right, but is there another way? i think this way is not the best – Fernando Santiago Jun 08 '12 at 17:48
  • You are asking how to access the EditText, and this is a way to do it in one line. Another way to do it would be to pass the EditText or String directly to your API (the last of which I prefer, to be honest) – HXCaine Jun 08 '12 at 17:50
  • I mean like activity.getAllWidgets().getEdtText(); or something like that – Fernando Santiago Jun 08 '12 at 17:50
  • The only decent way to retrieve a `View` from an activity is to use `findViewByID`. Otherwise you can create a getter method, but you'll have to pass through a ZebraPrinterActivity type rather than just an Activity. That's up to you, but I would recommend using `findViewByID` – HXCaine Jun 08 '12 at 17:53