Miasto
Miasto

Reputation: 33

Updating ArrayList Adapter

I think I bit off a little more than I can chew. I'm creating a dummy music player and I wanted to add a button that a user can add a Song() object to the ArrayList. The problem is I add the Song objects to the Array in the onCreate() method. I have a button that inflates a PopUpView and in that view I have another button that takes the getText() from the Edit Fields and adds an item to the ArrayList, when the 'done' button is tapped the PopUPView is dismissed and the new Song() appears in the song list, but when I go back to the main menu and then go back to the song list the new Object that the user adds is no longer there.

public class AllSongsActivity<name> extends AppCompatActivity {

ArrayList<Song> songs = new ArrayList<Song>();

private AlertDialog.Builder dialogBuilder;
private AlertDialog dialog;
private EditText songNameEditText;
private EditText artistNameEditText;
private EditText genreNameEditText;
private CheckBox isFavoriteCheckBox;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_all_songs);

    ListView listView = (ListView) findViewById(R.id.list);
    //Create List Of Song
    songs.add(new Song("Fever", "Dua Lipa", "POP", true));
    songs.add(new Song("Lonely", "Justin Bieber", "POP", false));
    songs.add(new Song("Courage To Change", "Sia", "POP", true));
    songs.add(new Song("Monster", "Shawn Mendes", "POP", false));
    songs.add(new Song("Dance Monkey", "Tones And I", "POP", true));
    songs.add(new Song("Masterpeice", "DaBaby", "RAP", false));
    songs.add(new Song("Wolves", "Big Sean", "RAP", false));
    songs.add(new Song("Trust", "Fivo Foreign", "RAP", false));
    songs.add(new Song("Back", "Jeezy", "RAP", false));
    songs.add(new Song("Bad Boy", "Juice Wrld", "RAP", true));
    songs.add(new Song("Deeper", "DubVision", "EDM", true));
    songs.add(new Song("Sick Of You", "Jerome", "EDM", true));
    songs.add(new Song("Paul Is Dead", "Scooter", "EDM", true));
    songs.add(new Song("All I Need", "Slushii", "EDM", true));
    songs.add(new Song("Willow", "Taylor Swift", "POP", false));

    ListAdapter songAdapter = new SongAdapter(this, songs);
    listView.setAdapter(songAdapter);

    Button addSongButton = (Button) findViewById(R.id.add_song_button);
    addSongButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popUpWindow();
        }
    });
}


public void popUpWindow() {
    dialogBuilder = new AlertDialog.Builder(this);
    final View addNewSongView = getLayoutInflater().inflate(R.layout.add_song_popup_window, null);
    dialogBuilder.setView(addNewSongView);
    dialog = dialogBuilder.create();
    int sizeOfArray = songs.size();
    System.out.println("This is the size of the array before songs added" + sizeOfArray);
    dialog.show();

    Button addNewSongButton = (Button)dialog.findViewById(R.id.done_button);
    addNewSongButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            songNameEditText = (EditText)dialog.findViewById(R.id.song_name_edit_text);
            artistNameEditText = (EditText)dialog.findViewById(R.id.arist_name_edit_text);
            genreNameEditText = (EditText)dialog.findViewById(R.id.genre_edit_text);
            isFavoriteCheckBox = (CheckBox)dialog.findViewById(R.id.favorite_checkbox);
            int songsSize = songs.size();

            String songName = songNameEditText.getText().toString();
            String artistName = artistNameEditText.getText().toString();
            String genreName = genreNameEditText.getText().toString();
            Boolean isFavorite = isFavoriteCheckBox.isSelected();

            if (songName.matches("") || artistName.matches("") || genreName.matches("")) {
                Toast.makeText(AllSongsActivity.this, "Please Fillout All Fields",Toast.LENGTH_SHORT).show();
            }else {
                    Song newObject = new Song(songName, artistName, genreName, isFavorite);
                    songs.add(newObject);
                    dialog.dismiss();
            }
        }
    });
}

I've been learning Android Development for a couple of months so I know my code is really messy and not using the best techniques.

Upvotes: 1

Views: 123

Answers (1)

Orest Fufalko
Orest Fufalko

Reputation: 26

The problem in your code: the songs list is a property of AllSongsActivity. If you close AllSongsActivity - all data from the activity and the songs list will simply disappear!

The simple, but not correct way to save a new song use static modifier for the songs: static ArrayList<Song> songs = new ArrayList<String>() {{ add(new Song("Fever", "Dua Lipa", "POP", true)); add(new Song("Lonely", "Justin Bieber", "POP", false)); }};

The correct way - learn how to store data in a database: https://developer.android.com/training/data-storage/room

Upvotes: 1

Related Questions