Using an MVC to delete an element from an arrayList

In summary, the conversation is about a problem with deleting a flashcard from an ArrayList. The person is having trouble with the position always being deleted and is seeking help to fix the issue. They have tried troubleshooting and looking for similar problems online, but have not been successful. The conversation also includes code from a flashcard model and controller, as well as attempts at fixing the problem.
  • #1
Smiles1
8
0
PLEASE HELP! How can I delete a flashcard from my ArrayList? My position never updates and it always deletes position 0. Please let me know if you need more code to examine. I've tried trouble shooting this to find the problem but I can't figure out how to fix it. It's bugging me so badly and I really want to understand it.

I've tried looking through stackOverflow for similar problems and have tried stepping through with the debugger.

Inside my flashCard Model
Code:
  public int getCurrentIndex(String displayText) {
        
        int position = 0;
        for (int i = 0; i < flashCards.size() - 1; i++) {
//Trying to see what's going on and why things aren't matching up
            System.out.println("Display Text:" + displayText);
            System.out.println(flashCards.get(i).getTextColor());
            System.out.println(position);
            if (flashCards.get(i).getTextColor().equals(displayText)) {
                
                position = i;
                break;
            }
            System.out.println(position);
        } 
       return position;
    }
    
//Does not work right, always deletes position zero.
    public void deleteFlashCard(String displayText) {
        this.flashCards.remove(getCurrentIndex(displayText));
        index = index--;
//Looking to see what was deleted
        System.out.println(this.flashCards.toString());
        
    }

}

Inside my flashcard controller

Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class FlashCardController {

    private FlashCardListUI flashCardListUI;
    private FlashCardList flashCardList;

    FlashCardController(FlashCardListUI flashCardListUI, FlashCardList flashCardList) {

        this.flashCardList = new FlashCardList();
        this.flashCardListUI = new FlashCardListUI();
        
        class NextFlashCardButtonListener implements ActionListener {

            public void actionPerformed(ActionEvent ae) {
                flashCardListUI.setCurrentFlashCard(flashCardList.getNextFlashCard().toString());
      
            }
        }
        class AddButtonListener implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                flashCardList.putFlashCard(flashCardListUI.getCurrentFlashCard());

            }
        }

        class BackButtonListener implements ActionListener {
//Is the problem here? If so, Why?
            public void actionPerformed(ActionEvent e) {
                flashCardListUI.setCurrentFlashCard(flashCardList.getPreviousFlashCard().toString());
                //flashCardList.putFlashCard(flashCardListUI.getCurrentFlashCard());

            }
        }
        
         class DeleteButtonListener implements ActionListener {

            public void actionPerformed(ActionEvent e) {

                flashCardList.deleteFlashCard(flashCardListUI.getCurrentFlashCard()); 
         
            }
        }
         
        flashCardListUI.nextFlashCardButtonListener(new NextFlashCardButtonListener());
        flashCardListUI.addFlashCardButtonListener(new AddButtonListener());
        flashCardListUI.backButtonListener(new BackButtonListener());
        flashCardListUI.deleteButtonListener(new DeleteButtonListener());
      
    }

}

Thank you for any hints or help! They are greatly appreciated!
 
Technology news on Phys.org
  • #2
Still need help!
This does not work either. :-(
Code:
public class FlashCardList {

    private ArrayList<FlashCard> flashCards;
    private FlashCard flashCard;
    private int index;
    private int position = -1;

    FlashCardList() {
        this.flashCards = new ArrayList();
        flashCards.add(new FlashCard("test"));
        index = 0;
     
    }

    public void putFlashCard(String textColor) {
        flashCard = new FlashCard(textColor);
        flashCards.add(flashCard);
        index = index++;
    }

    public FlashCard getFlashCard() {
        return flashCards.get(index);
    }

    public FlashCard getNextFlashCard() {
        index = index++;
        return flashCards.get(index);
    }

    public FlashCard getPreviousFlashCard() {
        index = index--;
        return flashCards.get(index);
    }

    public int getCurrentIndex(String displayText) {
        //return flashCards.indexOf(index);

        for (int i = 0; i < flashCards.size(); i++) {
            System.out.println("Display Text:" + displayText);
            System.out.println(flashCards.get(i).getTextColor());

            if (flashCards.get(i).getTextColor().equals(displayText)) {

                position = i;
                System.out.println(position);
                break;

            }

        }
        return position;
    }

//Does not work right
    public void deleteFlashCard() {
        //this.flashCards.remove(flashCards.get(getCurrentIndex(displayText)));
        this.flashCards.remove(index);
        index = 0;
        System.out.println("DELETE");
        System.out.println(flashCards);
        //System.out.println(this.flashCards.toString());
    }

}
 

What is an MVC?

An MVC, or Model-View-Controller, is a design pattern commonly used in software development to separate the application into three interconnected parts: the model, the view, and the controller. This allows for better organization and maintainability of code.

How do I delete an element from an arrayList using an MVC?

To delete an element from an arrayList using an MVC, you will need to first identify the specific element you want to delete and then call the appropriate method in the controller to remove it from the model. The view will then be updated to reflect the changes in the model.

What are the advantages of using an MVC to delete an element from an arrayList?

Using an MVC to delete an element from an arrayList has several advantages, including better organization and separation of concerns, easier maintenance and debugging, and improved scalability and flexibility.

Are there any potential drawbacks to using an MVC to delete an element from an arrayList?

One potential drawback of using an MVC to delete an element from an arrayList is that it may add additional complexity to the code, especially for simpler applications. Additionally, implementing an MVC may require more development time and resources.

Can I use an MVC to delete elements from any type of arrayList?

Yes, an MVC can be used to delete elements from any type of arrayList, as long as the appropriate methods and logic are implemented in the controller and model. This design pattern is not limited to a specific data type or structure.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
776
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
638
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
955
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top