Android 在Toast上显示ArrayList内容

Android 在Toast上显示ArrayList内容,android,eclipse,listview,arraylist,toast,Android,Eclipse,Listview,Arraylist,Toast,我有一个列表视图,我想打印包含所选项目的列表 我可以展示我每次选择的选择。i、 e.如果我选择了一个选项,我可以在toast中打印它(我在代码中将它标记为注释),但我希望将整个选项一起打印。 需要帮忙吗 谢谢..你的概念是正确的,但执行错误这是你错过的部分:` ArrayList<String> checked = new ArrayList<String>(); checked.add((position+1), listItem); Toast.makeText(ge

我有一个列表视图,我想打印包含所选项目的列表

我可以展示我每次选择的选择。i、 e.如果我选择了一个选项,我可以在toast中打印它(我在代码中将它标记为注释),但我希望将整个选项一起打印。 需要帮忙吗


谢谢..

你的概念是正确的,但执行错误这是你错过的部分:`

ArrayList<String> checked = new ArrayList<String>();
checked.add((position+1), listItem);
Toast.makeText(getApplicationContext(),checked.get((position+1)), Toast.LENGTH_LONG).show();`
ArrayList checked=new ArrayList();
选中。添加((位置+1),列表项);
Toast.makeText(getApplicationContext(),checked.get((位置+1)),Toast.LENGTH_LONG.show()`
您必须获取获取所需的ArrayList中元素的位置,因此
选中。get(元素的数组位置)

好吧,你的概念是正确的,但是错误的执行是你遗漏的部分:`

ArrayList<String> checked = new ArrayList<String>();
checked.add((position+1), listItem);
Toast.makeText(getApplicationContext(),checked.get((position+1)), Toast.LENGTH_LONG).show();`
ArrayList checked=new ArrayList();
选中。添加((位置+1),列表项);
Toast.makeText(getApplicationContext(),checked.get((位置+1)),Toast.LENGTH_LONG.show()`
您必须获取获取所需的ArrayList中元素的位置,因此
选中。获取(此处元素的数组位置)

如果要显示ArrayList中的每个项目,可以使用简单的循环并将它们添加到如下字符串中:

...
checked.add((position+1), listItem);

String tempString = "";
for(int x = 0; x < checked.length(); x++) {
    tempString = tempString + ", " + checked.get(x);
}
tempString = tempString.substring(2);
Toast.makeText(getApplicationContext(),tempString, Toast.LENGTH_LONG).show();
。。。
选中。添加((位置+1),列表项);
字符串tempString=“”;
对于(int x=0;x

编辑对其进行了一点修改,使其仅在项目之间添加逗号

如果要显示ArrayList中的每个项目,可以使用简单的循环并将其添加到如下字符串中:

...
checked.add((position+1), listItem);

String tempString = "";
for(int x = 0; x < checked.length(); x++) {
    tempString = tempString + ", " + checked.get(x);
}
tempString = tempString.substring(2);
Toast.makeText(getApplicationContext(),tempString, Toast.LENGTH_LONG).show();
。。。
选中。添加((位置+1),列表项);
字符串tempString=“”;
对于(int x=0;x

编辑对其进行了一些修改,使其仅在项目之间添加逗号

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

  • 正如donfuxx所说,您需要在onclicklistener之外创建arrayList
  • 当用户单击某个项目时,它将被添加到arrayList中
  • 然后在列表上循环以填充一个名为allItems的字符串,然后在祝酒词中显示allItems

    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();
        }
    });
    
    ArrayList checked=new ArrayList();
    setOnItemClickListener(新的OnItemClickListener(){
    @凌驾
    public void onItemClick(AdapterView父级、视图、,
    内部位置,长id){
    字符串listItem=(字符串)listView.getItemAtPosition(位置);
    如果(!checked.contains(listItem)){//可选:避免列表中重复的字符串
    选中。添加((位置+1),列表项);
    }
    字符串allItems=“;//用于在toast中显示
    for(字符串str:已选中){
    allItems=allItems+“\n”+str;//在项目之间添加新行
    }
    Toast.makeText(getApplicationContext(),allItems,Toast.LENGTH_LONG).show();
    }
    });
    

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

  • 正如donfuxx所说,您需要在onclicklistener之外创建arrayList
  • 当用户单击某个项目时,它将被添加到arrayList中
  • 然后在列表上循环以填充一个名为allItems的字符串,然后在祝酒词中显示allItems

    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();
        }
    });
    
    ArrayList checked=new ArrayList();
    setOnItemClickListener(新的OnItemClickListener(){
    @凌驾
    public void onItemClick(AdapterView父级、视图、,
    内部位置,长id){
    字符串listItem=(字符串)listView.getItemAtPosition(位置);
    如果(!checked.contains(listItem)){//可选:避免列表中重复的字符串
    选中。添加((位置+1),列表项);
    }
    字符串allItems=“;//用于在toast中显示
    for(字符串str:已选中){
    allItems=allItems+“\n”+str;//在项目之间添加新行
    }
    Toast.makeText(getApplicationContext(),allItems,Toast.LENGTH_LONG).show();
    }
    });
    

  • 但是我需要数组在其他地方使用它。这并不能消除数组,所以你可以在其他地方使用它。如果你想让所有内容再次出现在一个字符串中,只需再次运行循环或创建一个函数返回字符串。Toast看起来不太好,这是一个单独的问题。Toast消息不应该那么长,因此如果您的数组中有很多项,您可能需要使用警报对话框。如果您只是使用Toast进行调试,那么应该使用Log。否则我不确定,也许可以尝试使用
    YOURACTIVITYNAME。这是
    而不是
    getApplicationContext()
    。我只有2项。所以我认为toast是合适的,但是我需要数组在其他地方使用它。这并不能消除数组,所以你可以在其他地方使用它。如果你想让所有内容再次出现在一个字符串中,只需再次运行循环或创建一个函数返回字符串。Toast看起来不太好,这是一个单独的问题。Toast消息不应该那么长,因此如果您的数组中有很多项,您可能需要使用警报对话框。如果您只是使用Toast进行调试,那么应该使用Log。否则我不确定,也许可以尝试使用
    YOURACTIVITYNAME。这是
    而不是
    getApplicationContext()
    。我只有2项。所以toast是合适的我想你为什么删除了之前发布的代码?@donfuxx如果你需要它,它存在于下面的答案中我不明白你为什么删除了代码..为什么删除了之前发布的代码?@donfuxx如果你需要它,它存在于答案b中