我正在使用openCSV来读取csv文件,以便为我的Android应用程序充气我的自动完成功能。尽管来自csv的大多数值都已成功读取并且自动完成全文查看,但其中一些值缺失了。"例如,程序无法读取csv值" Los Angeles,CA",LAX 。
阅读CSV:
public void readCSV() throws IOException {
InputStream is = this.getAssets().open("airport-codes.csv");
InputStreamReader ifr = new InputStreamReader(is, "UTF-8");
CSVReader reader = new CSVReader (ifr);
ArrayList<String> srd = new ArrayList<>();
while ((reader.readNext()) != null)
{
nextLine = reader.readNext();
Log.i("ArrivalTest", nextLine[0] + "- " + nextLine[1]);
srd.add(nextLine[0] + " - " + nextLine[1]);
}
reader.close();
autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.originCityAutoComp1);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, srd);
autoCompleteTextView.setAdapter(adapter);
}
机场codes.csv:
...
"Lorient, France ",LRT
"Los Angeles, CA ",LAX // Los Angles LAX is in csv file.
"Los Cabos, Mexico ",SJD
...
logcat的:
...
Longview, TX - GGG
Lonorore, Vanuatu - LNE
Lord Howe Island, NS, Australia - LDH
Lorient, France - LRT
Los Cabos, Mexico - SJD // Los Angles LAX is missing.
Losuia, Papua New Guinea - LSA
Lourdes/Tarbes, France - LDE
...
答案 0 :(得分:3)
你的while循环正在从CSV文件中剔除每个奇数编号的数据行并将其丢弃:)
请改为尝试:
while ((nextLine = reader.readNext()) != null)
{
Log.i("ArrivalTest", nextLine[0] + "- " + nextLine[1]);
srd.add(nextLine[0] + " - " + nextLine[1]);
}