Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit 7eaa586

Browse files
author
Evgenii Kanivets
committed
#72[30m]. Change edit/delete options.
1 parent 98e89e3 commit 7eaa586

File tree

5 files changed

+37
-41
lines changed

5 files changed

+37
-41
lines changed

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/record/AddRecordActivity.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,36 @@ public boolean onCreateOptionsMenu(Menu menu) {
161161
return true;
162162
}
163163

164+
@Override
165+
public boolean onPrepareOptionsMenu(Menu menu) {
166+
switch (mode) {
167+
case MODE_ADD:
168+
menu.removeItem(R.id.action_delete);
169+
break;
170+
171+
case MODE_EDIT:
172+
break;
173+
174+
default:
175+
break;
176+
}
177+
178+
return true;
179+
}
180+
164181
@Override
165182
public boolean onOptionsItemSelected(MenuItem item) {
166183
switch (item.getItemId()) {
167184
case R.id.action_done:
168185
tryRecord();
169186
return true;
170187

188+
case R.id.action_delete:
189+
recordController.delete(record);
190+
setResult(RESULT_OK);
191+
finish();
192+
return true;
193+
171194
default:
172195
return super.onOptionsItemSelected(item);
173196
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/record/MainActivity.java

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import android.annotation.SuppressLint;
44
import android.content.Intent;
55
import android.support.v7.app.AppCompatActivity;
6-
import android.view.ContextMenu;
7-
import android.view.MenuItem;
86
import android.view.View;
9-
import android.widget.AdapterView;
107
import android.widget.ListView;
118
import android.widget.TextView;
129

@@ -38,6 +35,7 @@
3835

3936
import butterknife.Bind;
4037
import butterknife.OnClick;
38+
import butterknife.OnItemClick;
4139

4240
public class MainActivity extends BaseDrawerActivity {
4341
@SuppressWarnings("unused")
@@ -124,34 +122,13 @@ public void onPeriodSelected(Period period) {
124122
spinner.setPeriod(periodController.readLastUsedPeriod());
125123
}
126124

127-
@Override
128-
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
129-
super.onCreateContextMenu(menu, v, menuInfo);
130-
getMenuInflater().inflate(R.menu.menu_record, menu);
131-
}
132-
133-
@Override
134-
public boolean onContextItemSelected(MenuItem item) {
135-
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
136-
137-
switch (item.getItemId()) {
138-
case R.id.edit:
139-
// Minus one because of list view's header view
140-
Record record = recordList.get(info.position - 1);
141-
if (record.isIncome())
142-
startAddIncomeActivity(record, AddRecordActivity.Mode.MODE_EDIT);
143-
else startAddExpenseActivity(record, AddRecordActivity.Mode.MODE_EDIT);
144-
return true;
145-
146-
case R.id.delete:
147-
// Minus one because of list view's header view
148-
recordController.delete(recordList.get(info.position - 1));
149-
update();
150-
return true;
151-
152-
default:
153-
return super.onContextItemSelected(item);
154-
}
125+
@OnItemClick(R.id.list_view)
126+
public void editRecord(int position) {
127+
// Minus one because of list view's header view
128+
Record record = recordList.get(position - 1);
129+
if (record.isIncome())
130+
startAddIncomeActivity(record, AddRecordActivity.Mode.MODE_EDIT);
131+
else startAddExpenseActivity(record, AddRecordActivity.Mode.MODE_EDIT);
155132
}
156133

157134
@OnClick(R.id.btn_add_expense)
506 Bytes
Loading

app/src/main/res/menu/menu_add_record.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
<menu xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/apk/res-auto">
44

5+
<item
6+
android:id="@+id/action_delete"
7+
android:icon="@drawable/ic_delete"
8+
android:title="@string/delete"
9+
tools:showAsAction="ifRoom" />
10+
511
<item
612
android:id="@+id/action_done"
713
android:icon="@drawable/ic_done"

app/src/main/res/menu/menu_record.xml

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)