MHB Using an MVC to delete an element from an arrayList

  • Thread starter Thread starter Smiles1
  • Start date Start date
  • Tags Tags
    Delete Element
AI Thread Summary
The discussion revolves around a coding issue related to deleting a flashcard from an ArrayList in Java. The primary problem is that the deletion method always removes the flashcard at position 0, regardless of the intended target. The user has implemented a method, `getCurrentIndex`, to find the correct index based on the display text of the flashcard, but it fails to update the position correctly. The `deleteFlashCard` method attempts to remove the flashcard using the index returned by `getCurrentIndex`, but the logic for updating the index after deletion is flawed. The user has also expressed frustration after trying to troubleshoot the issue through debugging and searching for similar problems online. There are concerns about whether the issue lies within the button listeners in the controller, particularly regarding how the current flashcard is set before deletion. Overall, the key points of the discussion highlight the challenges in managing the index and ensuring the correct flashcard is targeted for deletion from the ArrayList.
Smiles1
Messages
7
Reaction score
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
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());
    }

}
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top