1
我試圖寫一個類,通過一個文本文件迭代,它看起來像這樣打開與getAssets)文件((這是〜5000行):麻煩裏面的AsyncTask
Postnr Poststad Bruksområde Kommunenummer Lat Lon Merknad Nynorsk Bokmål Engelsk
0001 Oslo Postboksar 301 59.91160 10.75450 Datakvalitet: 2. Koordinatar endra 28.09.2012. Oppdatert 04.12.2012 url1 url2 url3
我的問題是:該方法getassets是未定義的類型SearchTabTxt
我想從資產文件夾中讀取文件,我似乎無法找到解決方案。我試圖爲此編寫一個搜索類:
public class SearchTabTxt extends AsyncTask<String, Void, ArrayList<String[]>> {
protected ArrayList<String[]> doInBackground(String... inputString) {
ArrayList<String[]> list = new ArrayList<String[]>();
try {
InputStream is = getAssets().open("file.txt");
if (is != null) {
String search = inputString[0].toString();
InputStreamReader inputreader = new InputStreamReader(is,
"UTF-8");
BufferedReader buffreader = new BufferedReader(inputreader);
int antallTreff = 0;
while (buffreader.readLine() != null) {
ArrayList<String> placeInformation = new ArrayList<String>();
if (buffreader.readLine().contains(search)) {
antallTreff++;
System.out.println("Found: " + search);
placeInformation.clear();
for (String i : buffreader.readLine().split("\t")) {
placeInformation.add(i);
}
System.out.println(placeInformation.get(11));
// Sorry about the Norwegian will rewrite
if (antallTreff >= 3) {
System.out.println("Did I find something?");
break;
}
if (buffreader.readLine() == null) {
break;
}
}
}
}
} catch (ParseException e) {
Log.e("Error", e + "");
} catch (ClientProtocolException e) {
Log.e("Error", e + "");
} catch (IOException e) {
Log.e("Error", e + "");
}
return list;
}
}
感謝一堆,它現在像一個魅力工作,只是一個小調整,我可以像我想要的使用它(發現另一個錯誤,我沒有寫入列表,所以我沒有得到輸出):D –