Android:将ListView数据存储在Hashmap的ArrayList中<string,string =“”>

时间:2016-01-29 23:04:05

标签: java android listview arraylist hashmap

我正在为Android制作一个小型MP3播放器。 对于我的项目,我编写了一个类(MP3Finder),它返回HashMap的ArrayList中定位的歌曲。

到目前为止,我的应用只显示sdcard上的歌曲 在列表视图中,用户可以从中开始播放歌曲。

现在我希望用户能够将不同的歌曲添加到播放列表中。 出于这个原因,我使用包含所有内容的列表视图制作了一个歌曲列表活动 歌曲作为checkedTextView项目。 问题是我不知道如何存储歌曲列表中的已检查(由用户选择)歌曲 返回到HashMap的ArrayList,它将被发送到播放列表活动。 我希望你明白我想做什么。

以下是我的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();
        }
    }
}

任何帮助或建议都将不胜感激。

1 个答案:

答案 0 :(得分:1)

首先,您需要将ArrayList声明为静态,以便它可以跨活动持续存在。

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

现在点击按钮,您需要创建一个新的HashMap并将其放在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);
        }
    });

因此,只要单击SongListActivity中的按钮,它就会遍历for循环中的所有项目,并将检查的项目放入HashMap中。循环结束后,您可以在ArrayList中添加HashMap。