我有一个存储在共享首选项中的集合和另一个从数据库中提取的数组。当我将集合转换为数组并比较数组是否包含数据库中的数组时,它什么都不显示。我已经将每个数组的结果分别打印到控制台,我的值是现货。任何想法为什么这不起作用?
SharedPreferences prefs = getSharedPreferences("User", Context.MODE_PRIVATE);
final Set<String> greenSet = prefs.getStringSet("greenIngredients", null);
//pulls data_ingredients from database as a string, converts to array
String[] ingredientResult = data_ingredients.split(", ");
int val = ingredientResult.length;
LinearLayout ll = (LinearLayout) findViewById(R.id.greenLL);
final TextView[] myTextViews = new TextView[val]; // create an empty array;
String[] greenArray = greenSet.toArray(new String[greenSet.size()]);
if (!(greenSet ==null)) {
for (int i = 0; i < val; i++) {
final TextView rowTextView = new TextView(this);
rowTextView.setText(ingredientResult[i]);
if(Arrays.asList(greenArray).contains(ingredientResult[i])){
System.out.println("yes");
}
// add the textview to the linearlayout
ll.addView(rowTextView);
// save a reference to the textview for later
myTextViews[i] = rowTextView;
}
}
另外,正如旁注,这是在一个循环中发生的,它为从数据库中提取的数组中存在的每个项动态生成一个textview。
变量greenSet实际上是一个动态生成的集合,但它只是从应用程序的另一部分填充的一组项目。在这种情况下,控制台将greenSet打印为
I/System.out: [Beefeater Gin, ]
和ingredientResult as
I/System.out: [Beefeater Gin, 1 Scoop Pistachio Ice Cream, Root Beer]
答案 0 :(得分:0)