I am making a small MP3 Player for android. For my project I wrote a class (MP3Finder) that returns the located songs in an ArrayList of HashMap.
Until now, my app just displays the songs located on the sdcard in a listview from which an user can start playing a song.
Now I would like that a user should be able to add different songs to a playlist. For this reason I made a songlist activity with a listview that contains all the songs as checkedTextView items. The problem is that I don't know, how to store the checked (selected by the user) songs from the songlist back into an ArrayList of HashMap, which is to be send to the playlist activity. I hope you understand what I want to do.
Here is the source code of my SongListActivity:
public class SongListActivity extends AppCompatActivity {
// List of mp3 files
ArrayList<HashMap<String, String>> MP3List = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button isChecked = (Button) findViewById(R.id.btn_is_checked);
final ListView listView = (ListView) findViewById(R.id.list_view);
MP3Finder mp3Finder = new MP3Finder();
// Get all mp3's from sdcard
this.MP3List = mp3Finder.getMP3List();
// Add items to ListView
ListAdapter adapter = new SimpleAdapter(this, MP3List, R.layout.activity_item, new String[] { "mp3Title" }, new int[] {
R.id.mp3_title});
listView.setAdapter(adapter);
listView.setOnItemClickListener(new CheckBoxClick());
listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
isChecked.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SparseBooleanArray checked = listView.getCheckedItemPositions();
int len = listView.getCount();
for (int i = 0; i < len; i++) {
if (checked.get(i)) {
// ...here is the part, where I want to store the selected songs back
// in an ArrayList of HashMap...
}
}
}
});
}
public class CheckBoxClick implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView checkedTextView = (CheckedTextView) view;
if (checkedTextView.isChecked()) {
Toast.makeText(MainActivity.this, "checked", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "unchecked", Toast.LENGTH_SHORT).show();
}
}
}
Any help or suggestions would be appreciated.