-1

I am trying to access the edttxt value that exists in class Mainactivity, this is contained in class Mainactivity:

Button btn=(Button)findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText edttxt =(EditText)findViewById(R.id.edttxt);
                TextView txtview =(TextView)findViewById(R.id.textView);
                txtview.setText(edttxt.getText().toString());
            }
        });

The access I need is to replace the 100 values by edttxt value in the other class, this is part I need to modify the other class:

public void switchFlash() {
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                if (isFlashOn()) {
                    turnOffFlash();
                    x++;    
                } else if (x>20) {
                    turnOffFlash();
                }
                else
                {
                    turnOnFlash();
                }
                handler.postDelayed(this,100);
            }
        };
        handler.postDelayed(runnable,100);
    }
Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
Roberts
  • 101
  • 2
  • 9
  • 1
    And the question is? – Todor Kostov Aug 19 '16 at 11:28
  • declare static global variable and store your edit text value in it. Then access that static variable in any class wherever you want using `ClassName.yourStaticvariableName`. – jarvo69 Aug 19 '16 at 11:28
  • declare some static variable and store that value and try to access the same into your class – Rajendhiran Easu Aug 19 '16 at 11:29
  • Still you need more clarification, can you please explain the things in more detail and descriptive – Rajendhiran Easu Aug 19 '16 at 11:30
  • Pass variable in intent, do not use static variable for this. If you need how to do this I can share. – Ramit Aug 19 '16 at 11:31
  • @Ramit, I think passing the values through intent is a right way, but seeing to this code he is trying to access the value into the thread on the runtime but not sure what he exactly needed – Rajendhiran Easu Aug 19 '16 at 11:36
  • @Rajendhiran, I need to use the value from the Edittext to be the value of the delay in another class. i have put this in the Mainactivity class: "public static int edtxt;" the edtxt was colored gray saying that this variable is never used. – Roberts Aug 19 '16 at 11:38
  • @Aloweiwi can you do it or need some help for passing and using value in intent from one activity to another. – Ramit Aug 19 '16 at 11:39
  • @Ramit I am an absolute beginner and I don't even know what is an intent. – Roberts Aug 19 '16 at 11:40
  • Then please read about intent and check the answer http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android – Ramit Aug 19 '16 at 11:42
  • @Ramit Can't I achieve that using a static global variable ? – Roberts Aug 19 '16 at 11:45
  • You can achieve but its wrong buggy way. I suggested the correct way and its up to you to use shortcut or learn correct way. – Ramit Aug 19 '16 at 12:00

1 Answers1

0

As I understood from the above conversation and also from the context of your code what you want to pass to the other class is the value(string) contained in the EditText view and not just the view itself. In this case you have different options but one easy way to do it is to pass this 'value' to the other class at object creation using a constractor like the following:

Button btn=(Button)findViewById(R.id.btn);
final EditText edttxt =(EditText)findViewById(R.id.edttxt);
OtherClass otherClass = new OtherClass(delay);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        delay = edttxt.getText().toString();
        TextView txtview =(TextView)findViewById(R.id.textView);
        txtview.setText(edttxt.getText().toString());
    }
});

Declare delay as a global variable of the MainActivity class like:

public class MainActivity extends AppCompatActivity {

    private String delay;
    ....

And your other class may look like this:

public class OtherClass {
    int delay;
    public OtherClass(String delay){
        this.delay = Integer.parseInt(delay);
    }
    //...your code
    public void switchFlash() {
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                if (isFlashOn()) {
                    turnOffFlash();
                    x++;
                } else if (x>20) {
                    turnOffFlash();
                }
                else
                {
                    turnOnFlash();
                }
                handler.postDelayed(this,delay);
            }
        };
        handler.postDelayed(runnable,delay);
    }
}
Addis
  • 2,480
  • 2
  • 13
  • 21
  • I modified the code but when I upload it to the mobile the application stops working before it starts. – Roberts Aug 20 '16 at 08:46
  • There may be a bug in other parts of your code, so please provide it if possible so that we will be able to assist you. There is also an error message displayed in the logcat when your application stops which makes debugging very easy, so please provide that too. – Addis Aug 20 '16 at 11:40