user1127616
user1127616

Reputation:

how to acces main activity content in subclass in android

i have edit text on main activity i need to access this edit text on my sub class ......

this is my main activity

private EditText et1;
    private EditText et2, et;

// int dec = et.getText().toString().length()-1;

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

    et1 = (EditText) findViewById(R.id.editText1);
    et2 = (EditText) findViewById(R.id.editText2);
    et = et1;
    gLib = GestureLibraries.fromRawResource(this, R.raw.gestures);

    // glip1 = GestureLibraries.fromRawResource(this, R.raw.gestures1);

    if (!gLib.load()) {
        Log.w(TAG, "could not load gesture library");
        finish();
    }
    GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
    common myHandler = new common();
    gestures.addOnGesturePerformedListener(myHandler);
}

this is my class

public class common implements OnGesturePerformedListener {


    @Override
    public void onGesturePerformed(GestureOverlayView gestureView,
            Gesture gesture) {
        System.out.println("guster");

        ArrayList<Prediction> predictions = gLib.recognize(gesture);//i have to acces glip from main activity
        // ArrayList<Prediction> predictions1 = glip1.recognize(gesture);
        // one prediction needed
        if (predictions.size() > 0 && predictions.get(0).score > 2.0) {
            String prediction = predictions.get(0).name;
            // checking prediction

            if (prediction.equals("A")) {
                // and action
                et.append("A");// i have to access edit text from main activity
                // et.getText().insert(et.getSelectionStart(), "A");
            }
        }
    }
}

Upvotes: 0

Views: 2442

Answers (3)

Moog
Moog

Reputation: 10193

Forget trying to struggle with accessing members from outside the activity.

Just have your activity implement GesturePerformedListener

public class MyActivity implements OnGesturePerformedListener
{
  //your activity code here
}

Then instead of creating a common class when stting the listener just use this:

gestures.addOnGesturePerformedListener(this);

This removes the need for a seperate class and allows you to access your TextView directly

Upvotes: 0

Rahul Choudhary
Rahul Choudhary

Reputation: 3809

Easiest way would be to pass the main class / object reference into the subclass while initiating i.e. in constructor. And store the reference in member variable to use them later.

Upvotes: 0

MByD
MByD

Reputation: 137392

Pass a reference to the activity in the constructor, and add relevant methods to communicate between the classes (don't access the Activity's fields directly...)

In activity:

 common myHandler = new common(this);

In Common (used a capital C for name convention):

public class Common implements OnGesturePerformedListener {

    private YourActivityClass activity;

    public Common(YourActivityClass activity) {
        this.activity = activity;
    }
    // Rest of code

}

Upvotes: 3

Related Questions