【问题标题】:How to store and then pass an arrayList of all checked Items (HashMap objects items) from a ListView in Android?如何从Android中的ListView存储然后传递所有检查项目(HashMap对象项目)的arrayList?
【发布时间】:2013-03-20 12:27:45
【问题描述】:

大家好,我是 Android 编程新手,需要一些帮助。我有一个由 HashMap 对象组成的列表视图。可以通过选中复选框来选择这些 hashMap 对象。 (所以基本上我有一个复选框列表)。我想要做的是将 ListView 中检查的所有 HashMap 对象(或项目)存储在 ArrayList 中。然后,我想在按下按钮时将此 arrayList 传递给另一个活动。我见过一些人们存储字符串数据的例子,但我需要一些帮助来了解如何将 listView 中的“已检查”HashMap 对象存储到 ArrayList 中。所以最后我想要的是一个 ArrayList,它由在列表中检查的对象(项目)组成,然后我可以传递。

程序目标是:允许用户根据类别选择歌曲并创建自己的自定义播放列表。将向用户呈现一个类别的歌曲,他们将从该类别中选择他们想要的歌曲,然后按下“下一步”按钮,这会将他们发送到一个新的活动,在那里他们将看到一个新的歌曲类别。在每个类别中,我都试图存储他们的选择并将其从活动(类别)传递到活动(类别)。但是传递不起作用。我可能正在做一些事情。需要一些建议什么或如何做。 -谢谢

一些代码:不是我所知道的最有条理的。 android新手,所以我应该改变任何指针或东西

   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playlist);

    //final ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();

    SongsManager plm = new SongsManager();
    // get all songs from sdcard
    this.songsList = plm.getPlayList();
    TextView label = new TextView(this);
    label.setText("Warm-up");
    label.setTextSize(16);
    label.setBackgroundColor(Color.WHITE);
    label.setTextColor(Color.BLACK);

    final ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
    // looping through playlist
    for(HashMap hs: songsList){
     if(hs.containsValue("Warm-up/cool down"))
    songsListData.add(hs);
     }

    // NEXT button
    Button btnLoadMore = new Button(this);
    btnLoadMore.setText("Next");
    ListView lv = getListView();
    lv.addHeaderView(label);

// Adding Load More button to lisview at bottom
      lv.addFooterView(btnLoadMore);

    //List<Model> selectedlist = getModel(songsListData);
    ArrayAdapter<Model> adapter = new InteractiveArrayAdapter(this,
        getModel(songsListData));

    setListAdapter(adapter);

    //Listening to Load More button click event

    btnLoadMore.setOnClickListener(new View.OnClickListener() {
    @Override
public void onClick(View arg0) {
      String priorSongs = "priorSongs";
      List<Model> selSongs = getModel(songsListData);
      Intent i = new Intent(WarmUp.this, SlowWalking.class);
      i.putExtra(priorSongs, selSongs);
      startActivity(i); 
}
});

    lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
      int position, long id) {

     // getting listitem index
    int songIndex = position;

    // Starting new intent
    Intent in = new Intent(getApplicationContext(),
                        AndroidBuildingMusicPlayerActivity.class);
    in.putExtra("songIndex", songIndex);
    startActivity(in);
    finish();
            }
        });

    }
    private List<Model> getModel(ArrayList<HashMap<String, String>> songsListData) {
        List<Model> list = new ArrayList<Model>();
        for (HashMap<String, String> map : songsListData)
            for (Entry<String, String> entry : map.entrySet())
                 if (entry.getKey() == "songTitle")
                     list.add(get(entry.getValue()));

        // Initially select one of the items
        //list.get(1).setSelected(true);
        return list;
      }

【问题讨论】:

  • 但是传递不起作用 - 它究竟是如何失败的?
  • 我认为这与 i.putExtra() 方法有关。我正在尝试传入一个字符串和 Serializable 对象。但是 List 没有实现 Serializable。

标签: android listview checkbox arraylist android-arrayadapter


【解决方案1】:

您必须创建一个扩展 SimpleAdapter 的适配器类,以便您可以将所有哈希映射放在列表视图中。

【讨论】:

    猜你喜欢
    • 2013-02-17
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2012-07-14
    • 2017-12-21
    相关资源
    最近更新 更多