我试图弄清楚为什么我的列表视图中的项目显示的顺序错误。我认为这与迭代器有关,但我不确定。
这是我活动的代码:
//String Capitole
String Articolul1 = getResources().getString(R.string.dispozitiiGeneraleArt_1);
String Articolul2 = getResources().getString(R.string.dispozitiiGeneraleArt_2);
String Articolul3 = getResources().getString(R.string.dispozitiiGeneraleArt_3);
//ListView Set
ListView listDispGen = (ListView) findViewById(R.id.dispozitiigeneralelist);
HashMap<String, String> articolExplicatii = new HashMap<>();
articolExplicatii.put(" Art.1", Articolul1);
articolExplicatii.put(" Art.2", Articolul2);
articolExplicatii.put(" Art.3", Articolul3);
List<HashMap<String, String>> listItems = new ArrayList<>();
SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.dispgeneralelist,
new String[]{"First Line", "Second Line"},
new int[]{R.id.main_item, R.id.sub_item});
Iterator it = articolExplicatii.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());
listItems.add(resultsMap);
}
listDispGen.setAdapter(adapter);
主要问题是,当我运行应用程序时,项目不会以正确的顺序显示,如: Art.1,Art.2 .... Art.9但它们随机展示,如Art.3,Art.1,Art.3等。