protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (resultCode != RESULT_OK) return; Bundle bundle = intent.getExtras(); if (bundle == null) return; String front = bundle.getString(AddCards.CARD_FRONT); String back = bundle.getString(AddCards.CARD_BACK); if (requestCode == ADD_CARD) { Cards newCard = new Cards(front, back); cards.add(newCard); backend.save(deck); } if (requestCode == EDIT_CARD) { String oldCardFront = bundle.getString(AddCards.OLD_CARD_FRONT); deck.deleteCard(oldCardFront); Cards newCard = new Cards(front, back); deck.addCard(newCard); backend.save(deck); cards = deck.getCards(); } listView.setAdapter(new ArrayAdapter<Cards>(this, R.layout.card, cards)); }
public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); Cards currCard = (Cards) getListView().getItemAtPosition(info.position); switch (item.getItemId()) { case R.id.edit: Intent intent = new Intent(this, AddCards.class); Bundle bundle = new Bundle(); bundle.putString(DECK_TITLE, deck.getName()); bundle.putString(AddCards.OLD_CARD_FRONT, currCard.getFront()); bundle.putString(AddCards.OLD_CARD_BACK, (String) currCard.getBack()); intent.putExtras(bundle); startActivityForResult(intent, EDIT_CARD); return true; case R.id.delete: deck.deleteCard(currCard.getFront()); backend.save(deck); cards = deck.getCards(); listView.setAdapter(new ArrayAdapter<Cards>(this, R.layout.card, cards)); return true; default: return super.onContextItemSelected(item); } }