1

I'm trying to write a class that iterates through a textfile, it looks like this (it's ~5000 lines):

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 

My trouble is: the method getassets is undefined for the type SearchTabTxt

I'm trying to read the file from the assets folder and I can't seem to find a solution to this. I tried to write a search class for this:

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; } } 

1 Answer 1

1

Well it's simple. There is no method getAssets() in your SearchTabTxt class. To get the assets, you need a Context. Make a public constructor to your SearchTabTxt class, and pass a Context.

private Context context; public SearchTabTxt (Context myContext) { this.context = myContext; } 

Now in the doINbackground method you can do:

InputStream is = context.getAssets().open("file.txt"); 

Now in the Activity when you createyour AsyncTask you can start the task like this: new SearchTabTxt(this).execute(params); This works because your Activity (this) is a subtype of Context. More on this here: getAssets(); from another class

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a bunch, it's working like a charm now, just a little tuning and I can use it like I want to (found another error, I didn't write to the list, so I didn't get an output) :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.