So I'm planning on creating a simple 'no-db' todo app
I've been able to display and add items to my list. Now I'm trying to delete by passing the index of the clicked item. The problem is that I don't know how to pass parameters. Any help would be greatly appreciated!
Here's my code
public class MainActivity extends AppCompatActivity {
List<String> notesList;
Toolbar toolbar;
ListView listNotes;
ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(Color.WHITE);
notesList = new ArrayList<String>();
// notesList.add("1");
listNotes = findViewById(R.id.list_notes);
delete = findViewById(R.id.btn_delete);
loadNotesList();
}
private void loadNotesList() {
if(mAdapter == null){
mAdapter = new ArrayAdapter<String>(this,R.layout.note_item,R.id.note_title, notesList);
listNotes.setAdapter(mAdapter);
}
else {
mAdapter.clear();
mAdapter.addAll(notesList);
mAdapter.notifyDataSetChanged();
}
}
private void displayToast() {
// notesList.add("test");
Log.d("list", notesList.toString());
Toast.makeText(getApplicationContext(), notesList.toString(),
Toast.LENGTH_LONG).show();
}
private void deleteNote(View view){
View parent = (View)view.getParent();
TextView noteTitleTextView = (TextView)findViewById(R.id.note_title);
String noteTitle = String.valueOf(noteTitleTextView.getText());
Toast.makeText(getApplicationContext(), noteTitleTextView.toString(),
Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
notesList = new ArrayList<String>();
//notesList.add("tset vissew");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_actions, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_add) {
displayToast();
final EditText noteEditText = new EditText(this);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add a new note")
.setMessage("What's on your mind?")
.setView(noteEditText)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String note = String.valueOf(noteEditText.getText());
notesList.add(note);
loadNotesList();
displayToast();
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
//loadNotesList();
//displayToast();
}
return super.onOptionsItemSelected(item);
}
}
My layout for the list item
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:id="@+id/note_title"
android:text="Example"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:gravity="center"/>
<Button
android:id="@+id/btn_delete"
android:text="Delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
The code loooks really ugly, I'm sorry for that.