在Toast上显示ArrayList内容 [英] Showing an ArrayList content on a Toast

查看:102
本文介绍了在Toast上显示ArrayList内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个listView,并且我想打印包含选定项目的arrrayList.

I have a listView and I want to print the arrrayList which contains the selected items.

我可以显示每次选择的选择.即,如果我选择一个选项,则可以将其打印在烤面包上(我在代码中将其标记为注释),但是我想将所有选项一起打印. 有什么帮助吗?

I can show the choice that I choose every time. i.e. if I select a choice, I can print it in a toast (I mark it in my code as a comment), but I want to print the whole choices together. Any help please?

谢谢..

推荐答案

如果我理解正确,则希望在Toast中显示arrayList的内容.

If I understand correctly, you want to display the contents of your arrayList in a Toast.

  1. 就像donfuxx所说,您需要在onclicklistener之外创建arrayList.
  2. 当用户单击一个项目时,该项目将被添加到您的arrayList中.
  3. 然后在列表上循环以填充名为allItems的字符串,然后在烤面包中显示allItems.

  1. Like donfuxx said, you need to create your arrayList outside of your onclicklistener.
  2. As the user clicks an item, it will be added to your arrayList.
  3. Then loop over the list to fill a string called allItems, then show allItems in a toast.

ArrayList<String> checked = new ArrayList<String>();

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

        String listItem = (String) listView.getItemAtPosition(position);

        if(!checked.contains(listItem)){ //optional: avoids duplicate Strings in your list
            checked.add((position+1), listItem);
        }
        String allItems = ""; //used to display in the toast

        for(String str : checked){
          allItems = allItems + "\n" + str; //adds a new line between items
        }

        Toast.makeText(getApplicationContext(),allItems, Toast.LENGTH_LONG).show();
    }
});

这篇关于在Toast上显示ArrayList内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆