1

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.

2
  • shouldn't it be Hashmap<String,ArrayList> where string will be playlist name and ArrayList will contain the list of songs ? Commented Jan 29, 2016 at 23:10
  • Hello Anshul. No, in my project I used HashMap<String, String> where the first string (key) should be the path of the song and the second string should be the artist and the titel of the song. Maybe you didn't understand what exactly I want to know. To populate the ListView items with data from the ArrayList I used a SimpleAdapter. What I want to know is the way back. What I mean is how to populate an ArrayList of Hashmap<String, String> from the listview. Please could you explain me how to do that? Maybe with a small example. Commented Jan 30, 2016 at 9:23

1 Answer 1

1

First of all you need to declare your ArrayList as static so that it could persist across activities.

 static ArrayList<HashMap<String, String>> MP3List = new ArrayList<>();

Now on button click, you need to create a new HashMap and put it inside MP3List

isChecked.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            HashMap<String,String> hashmap = new HashMap();

            SparseBooleanArray checked = listView.getCheckedItemPositions();

            int len = listView.getCount();

            for (int i = 0; i < len; i++) {
                if (checked.get(i)) {
                   String path = //Get your path here;
                   String title = // Get your title here;
                   hashmap.put(path,title);
                }
            }
          MP3list.add(hashmap);
        }
    });

So as soon as you click a button in SongListActivity, it will go through all the items in a for loop and put checked items in the HashMap. When the loop is over, you can add the HashMap inside the ArrayList.

Sign up to request clarification or add additional context in comments.

3 Comments

are you able to complete your activity with this approach ?
Unfortunately not. The problem is, that I don't know how to retrieve the data separately from the ListView. As I have already mentioned, I have populated the ListView from an ArrayList of HashMap<String, String>. So the items in the ListView are stored in the following manner: {mp3Path=/storage/emulated/0/artist - song.mp3, mp3Title=artist - song}. How can I get the path and the title separately from the ListView?
I solved the problem, how to get the path and the title separately from the ListView. I have splitted the string into two elements of a string array. Then I was able to pass the elements to the HashMap. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.