So far, I have a basic understanding of intents and have the following code for the ADD button in the MainActivity:
Button addBtn = (Button) findViewById(R.id.btnAdd);
addBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText txtName = (EditText) findViewById(R.id.txtName);
String name = txtName.getText().toString();
Intent intent = new Intent(MainActivity.this, AllTasksActivity.class);
startActivityForResult(intent, lastKeyID);
if (name.trim().length() > 0) {
lastKeyID++;
ToDo toDo = new ToDo();
toDo.id = lastKeyID;
toDo.name = name;
arrayList.add(toDo);
adapter.notifyDataSetChanged();
SharedPreferences preferences = mContext.getSharedPreferences("myPrefsKeys", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(toDo.id.toString(), toDo.name);
editor.apply();
txtName.setText("");
}
}
});
And I have the following code in my AllTasksActivity (the activity to display the to-do item(s) sent from the MainActivity):
List< ToDo > arrayList;
Integer lastKeyID = 0;
public static final String ADD_TODO_ITEM = "com.example.todolist.alltasks";
Intent intent = new Intent();
intent.putExtra(ADD_TODO_ITEM, lastKeyID);
setResult(RESULT_OK, intent);
finish();
Where am I going wrong or what do I need to do to make this code work? All I want to do is display the to-do item in the AllTasksActivity when the ADD button is pressed. As of this post, the app crashes when the ADD button is pressed.