我正在尝试使用哈希图通过正在制作的实践应用程序中的片段在列表视图中显示某些数据。哈希图以前工作过,但是当我切换到片段时,出现错误。我正在使用片段时是否需要以某种方式重写此代码?
我在出现问题的地方附加了代码,但是在新的SimpleAdapter括号中出现了错误标志
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(layout.home_fragment, null);
ListView recipesListView = (ListView) view.findViewById(id.recipesListView);
HashMap<String, String> recipes = new HashMap<>();
recipes.put("Bangers and Mash", "Sausages, Potatoes, Gravy \n\nCook the sausages in a pan and boil the potatoes then mash them put on a plate and pur gravy on them");
recipes.put("Spaghetti Carbonara", "Bacon, Onions, Spaghetti, Eggs, Cream \n\nCook bacon, onions in a pan and boil spaghetti, once cooked pour in eggs and cook on high until ready");
recipes.put("Chicken and Bacon Sandwich", "Chicken, Bacon, Mayonnaise, Lettuce, Bread, Butter\n\nButter the bread and spread mayonnaise on it. Put the chicken and bacon between the bread");
List<HashMap<String, String>> listRecipes = new ArrayList<>();
SimpleAdapter adapter = new SimpleAdapter(this, listRecipes, layout.list_items,
new String[]{"First Line", "Second Line"},
new int[]{R.id.recipeTitle, R.id.method});
Iterator it = recipes.entrySet().iterator();
while (it.hasNext()) {
HashMap<String, String> resultsMap = new HashMap<>();
Map.Entry pair = (Map.Entry) it.next();
resultsMap.put("First Line", pair.getKey().toString());
resultsMap.put("Second Line", pair.getValue().toString());
listRecipes.add(resultsMap);
}
recipesListView.setAdapter(adapter);
return view;
}
}
}
感谢您能提供的任何帮助。