嘿 stackoverflow 社区,
我想通过共享首选项将 Textviews 的 Arraylist 发送到另一个活动,因此我尝试将我的列表转换为 HashSet。
这是不被接受的:
public List<TextView> FavDishes = new ArrayList<TextView>();
. . . FavDishes = new ArrayList<>(); FavDishes.add(eingabe);
**Set<String> taskSet = new HashSet<String>(FavDishes);**
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putStringSet("Data", taskSet)
.apply();
它告诉我:“无法在 android 小部件 Textview 上解析构造函数 HashSet”
你会如何解决这个问题?
谢谢你的时间。
迭代 FavDishes 数组以将 TextView 值作为字符串获取并将它们存储到一个数组中,然后将该数组传递给您的任务集而不是 FavDishes。
代码将类似于:
List<TextView> FavDishes = new ArrayList<TextView>();
FavDishes.add(fav1TV);
FavDishes.add(fav2TV);
FavDishes.add(fav3TV);
ArrayList<String> FavDishesString = new ArrayList<String>();
for (TextView FavDish : FavDishes ){
FavDishesString.add(FavDish.getText().toString());
}
Set<String> taskSet = new HashSet<String>(FavDishesString);
迭代 arrayList 方法的有用链接:
https://crunchify.com/how-to-iterate-through-java-list-4-way-to-iterate-through-loop/
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句