Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 获取要按顺序打印的哈希映射_Android_Listview_Hashmap - Fatal编程技术网

Android 获取要按顺序打印的哈希映射

Android 获取要按顺序打印的哈希映射,android,listview,hashmap,Android,Listview,Hashmap,我使用HashMap从2列CSV文件中检索了数据。这是用于字典样式的应用程序-一列包含术语,第二列包含定义,它们通过HashMap链接到术语 我的应用程序做的第一件事就是将术语列表打印成一个列表。然而,它们似乎都是以随机顺序出现的 我希望它们保持与CSV文件中相同的顺序(我不依赖任何字母排序方法,因为我偶尔会使用非标准字符,更喜欢在源代码处按字母排序) 这是我的代码,它从CSV文件中提取数据并将其打印到列表中: String next[] = {}; // 'next' is used to

我使用HashMap从2列CSV文件中检索了数据。这是用于字典样式的应用程序-一列包含术语,第二列包含定义,它们通过HashMap链接到术语

我的应用程序做的第一件事就是将术语列表打印成一个列表。然而,它们似乎都是以随机顺序出现的

我希望它们保持与CSV文件中相同的顺序(我不依赖任何字母排序方法,因为我偶尔会使用非标准字符,更喜欢在源代码处按字母排序)

这是我的代码,它从CSV文件中提取数据并将其打印到列表中:

  String next[] = {}; // 'next' is used to iterate through dictionaryFile
  final HashMap<String, String> dictionaryMap = new HashMap<String, String>(); // initialise a hash map for the terms

  try {
        CSVReader reader = new CSVReader(new InputStreamReader(getAssets().open("dictionaryFile.csv")));
        while((next = reader.readNext()) != null) { // for each line of the input file
            dictionaryMap.put(next[0], next[1]); // append the data to the dictionaryMap
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

  String[] terms = new String[dictionaryMap.keySet().size()]; // get the terms from the dictionaryMap values
  terms = dictionaryMap.keySet().toArray(terms);

  setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, terms));
  ListView lv = getListView();
stringnext[]={};/'“下一步”用于迭代字典文件
final HashMap dictionaryMap=新HashMap();//初始化术语的哈希映射
试一试{
CSVReader reader=新的CSVReader(新的InputStreamReader(getAssets().open(“dictionaryFile.csv”));
而((next=reader.readNext())!=null){//用于输入文件的每一行
dictionaryMap.put(next[0],next[1]);//将数据追加到dictionaryMap
}
}捕获(IOE异常){
e、 printStackTrace();
}
String[]terms=新字符串[dictionaryMap.keySet().size()];//从字典映射值中获取术语
terms=dictionaryMap.keySet().toArray(术语);
setListAdapter(新的ArrayAdapter(此,R.layout.list_项,术语));
ListView lv=getListView();

这会导致应用程序加载,并且术语已就位,但它们的顺序完全不清楚。我如何让他们按照原来的顺序打印?

问题是,普通的
哈希映射不能保证顺序
此类不保证映射的顺序;特别是,它不能保证订单在一段时间内保持不变。

尝试使用
LinkedHashMap
,它将保持插入顺序

从文档-
哈希表和映射接口的链表实现,具有可预测的迭代顺序

以下是文档的链接-