简体   繁体   中英

Android: Store ListView data in an ArrayList of Hashmap<String, String>

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM