1

I have declared my variable, 'changed' too null so that I can check if it changes when the edittext is changed,

The problem I think is, when I press either the save button or the cancel button, it will always produce the value null, as upon clicking the button it is still null. However, I thought that the textwatcher would listen to the EditText and even if nothing was changed in the EditText it would by default change the SetChanged() to false as it provided "live updates", however clearly this isn't the case, am I doing something wrong? or am I supposed to approach it in a different way?, is there some way of refreshing it?

Advise would be greatly appreciated.

(P.S Some code was deleted to reduce the size and make it look easy on the eye, so excuse me for any missing braces. Furthermore, the activity does run properly as it shows the layout.However upon pressing any of the buttons it causes it to crash.)

public class EditNewItemActivity extends AppCompatActivity{

private Boolean changed = null;
private TextView title,content;
private Button saveBtn,cancelBtn;
private String date;
private int id;


@Override

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_item);

    title = (TextView) findViewById(R.id.editItemTitle);
    content = (TextView) findViewById(R.id.editItemDescription);
    saveBtn = (Button) findViewById(R.id.editItemSaveBtn);
    cancelBtn = (Button) findViewById(R.id.editItemCancelBtn); 

    Bundle extras = getIntent().getExtras();
    title.setText(extras.getString("title"));
    content.setText(extras.getString("content"));

    date = extras.getString("date");
    id = extras.getInt("id");

    GenericTextWatcher textWatcher = new GenericTextWatcher();

    title.addTextChangedListener(textWatcher);
    content.addTextChangedListener(textWatcher);

    ClickEvent clickEvent = new ClickEvent();

    saveBtn.setOnClickListener(clickEvent);
    cancelBtn.setOnClickListener(clickEvent);


}

private class ClickEvent implements View.OnClickListener{

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.editItemSaveBtn:

                save();

                break;

            case R.id.editItemCancelBtn:

                cancel();
                break;
        }
    }
}

private void cancel() {

    if (getChanged() == null){

    //This was used to simply verify that getchanged was still null.
    }

    if (title.getText().toString() != "" || content.getText().toString() != ""){


        if (getChanged() == false) {

 // if nothing has been changed let it cancel etc
        }else {

        }
    }
}

private void save() {

    if (tempTitle != "" || tempContent != "") {

        if(getChanged() == true){
 }

}

public Boolean getChanged() {
    return changed;
}

public void setChanged(Boolean changed) {
    this.changed = changed;
}

private class GenericTextWatcher implements TextWatcher{

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        Log.v("Beforetext:", s.toString());
        EditNewItemActivity editItem = new EditNewItemActivity();

        editItem.setChanged(false);

    }



    @Override
    public void afterTextChanged(Editable s) {

        Log.v("afterTextChanged:", s.toString());

        EditNewItemActivity editItem = new EditNewItemActivity();

        editItem.setChanged(true);

        Log.v("Status:", editItem.getChanged().toString());

    }
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

1 Answers1

0

You had change the changed. But which you changed is in your new EditNewItemActivity not in your current page.

This is where you made mistake (beforeTextChanged and afterTextChanged in your GenericTextWatcher):

EditNewItemActivity editItem = new EditNewItemActivity();
editItem.setChanged(false);   //or true

You should just call:

setChanged(false);     // or true

In fact, you should not new an activity yourself, activity must be create by the Android Framework so that it can be managed by the system.

L. Swifter
  • 3,179
  • 28
  • 52
  • I have tried your suggestion, but it has had no effect. The changed variable remains null despite, changing the EditText or not changing it. – MedicoreProgrammer Aug 15 '16 at 10:49