Monday 16 January 2012

Android problem with ArrayList of String

I was trying to read a text file to an ArrayList of String. The code is like this:

public class SymptomCategory {
private ArrayList<String> mCategory;
public SymptomCategory (InputStream is) {
String str="";
StringBuffer buf = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
if (is!=null) {
try {
while ((str = reader.readLine()) != null) {
System.out.println(str);
mCategory.add(new String(str));
buf.append(str + "\n" );
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public ArrayList<String> getCategory() {
return mCategory;
}
}

The problem was that I can only read the first line of the file.

I fixed the problem by adding the following at the beginning of the constructor:

mCategory = new ArrayList<String>();

No comments:

Post a Comment