本文介绍了在 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.
- 就像 donfuxx 所说,您需要在 onclicklistener 之外创建 arrayList.
- 当用户单击一个项目时,它将被添加到您的 arrayList 中.
然后遍历列表以填充名为 allItems 的字符串,然后在 Toast 中显示 allItems.
- Like donfuxx said, you need to create your arrayList outside of your onclicklistener.
- As the user clicks an item, it will be added to your arrayList.
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 内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!