Created
May 22, 2020 10:15
-
-
Save thundercipher/d3d7768bdc88deb2f1eab65d6181c0f5 to your computer and use it in GitHub Desktop.
Code of the NoteEditorActivity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.appcompat.app.AppCompatActivity; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.os.Bundle; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.widget.EditText; | |
import java.util.HashSet; | |
public class NoteEditorActivity extends AppCompatActivity { | |
int noteID; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_note_editor); | |
EditText editText = (EditText)findViewById(R.id.editText); | |
Intent intent = getIntent(); | |
noteID = intent.getIntExtra("noteID", -1); | |
if(noteID != -1) | |
{ | |
editText.setText(MainActivity.notes.get(noteID)); | |
} | |
else | |
{ | |
MainActivity.notes.add(""); // as initially, the note is empty | |
noteID = MainActivity.notes.size() - 1; | |
MainActivity.arrayAdapter.notifyDataSetChanged(); | |
} | |
editText.addTextChangedListener(new TextWatcher() | |
{ | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) | |
{ | |
} | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) | |
{ | |
MainActivity.notes.set(noteID, String.valueOf(s)); | |
MainActivity.arrayAdapter.notifyDataSetChanged(); | |
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.tanay.thunderbird.deathnote", Context.MODE_PRIVATE); | |
HashSet<String> set = new HashSet<>(MainActivity.notes); | |
sharedPreferences.edit().putStringSet("notes", set).apply(); | |
} | |
@Override | |
public void afterTextChanged(Editable s) | |
{ | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment