【发布时间】:2021-04-25 04:48:40
【问题描述】:
我觉得我咬的有点多,我不能咀嚼。我正在创建一个虚拟音乐播放器,我想添加一个按钮,用户可以将 Song() 对象添加到 ArrayList。问题是我在 onCreate() 方法中将 Song 对象添加到 Array 中。我有一个膨胀 PopUpView 的按钮,在该视图中我有另一个按钮,它从编辑字段中获取 getText() 并将一个项目添加到 ArrayList,当点击“完成”按钮时,PopUPView 被关闭并且新歌曲() 出现在歌曲列表中,但是当我返回主菜单然后返回歌曲列表时,用户添加的新对象不再存在。
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();
}
}
});
}
我已经学习了几个月的 Android 开发,所以我知道我的代码真的很乱,而且没有使用最好的技术。
【问题讨论】:
-
也许,您每次从主菜单进入时都会创建一个
AllSongsActivity的新实例,然后songs列表被初始化为空ArrayList和onCreate()被称为含义您将只有您在onCreate()方法中添加的歌曲。 -
有没有更好的地方来初始化ArrayList?我觉得应该有一种方法可以将它初始化为全局变量并引用它,就像我说我是 Android Dev 的新手,但我觉得这对于使用 Swift 的 IOS 应用程序来说非常容易。跨度>
-
不,我认为您需要初始化对象
AllSongsActivity的变量并保留对它的引用,因为它可以让您存储更改。你知道我的意思吗?
标签: java android android-studio arraylist