【问题标题】:Android: Store ListView data in an ArrayList of Hashmap<String, String>Android:将 ListView 数据存储在 Hashmap<String, String> 的 ArrayList 中
【发布时间】:2016-05-07 20:20:06
【问题描述】:

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

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

现在我希望用户能够将不同的歌曲添加到播放列表中。 出于这个原因,我使用包含所有 歌曲作为checkedTextView 项目。 问题是我不知道如何存储歌曲列表中选中的(用户选择的)歌曲 返回到 HashMap 的 ArrayList 中,该 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();
        }
    }
}

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

【问题讨论】:

  • 不应该是 Hashmap 其中 string 将是播放列表名称,而 ArrayList 将包含歌曲列表?
  • 你好安舒尔。不,在我的项目中,我使用了 HashMap 其中第一个字符串(键)应该是歌曲的路径,第二个字符串应该是艺术家和歌曲的标题。也许你不明白我到底想知道什么。为了使用来自 ArrayList 的数据填充 ListView 项目,我使用了 SimpleAdapter。我想知道的是回来的路。我的意思是如何从列表视图中填充 Hashmap 的 ArrayList。请你能解释一下我该怎么做吗?也许举个小例子。

标签: java android listview arraylist hashmap


【解决方案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。

【讨论】:

  • 你能用这种方法完成你的活动吗?
  • 很遗憾没有。问题是,我不知道如何从 ListView 中单独检索数据。正如我已经提到的,我从 HashMap 的 ArrayList 填充了 ListView。所以ListView中的item的存储方式如下:{mp3Path=/storage/emulated/0/artist - song.mp3, mp3Title=artist - song}。如何从 ListView 中分别获取路径和标题?
  • 我解决了问题,如何从ListView中分别获取路径和标题。我已将字符串拆分为字符串数组的两个元素。然后我能够将元素传递给 HashMap。 :-)
猜你喜欢
  • 1970-01-01
  • 2012-06-18
  • 1970-01-01
  • 2013-05-07
  • 1970-01-01
  • 1970-01-01
  • 2011-12-31
  • 2023-03-16
相关资源
最近更新 更多