OKay so I’m handling Files where I ask user Input and my program updates the Spinner with new selection.
Here’s the codes for write:
public void writeOnFile(String string){
try {
FileOutputStream file = openFileOutput(fileName, Context.MODE_APPEND);
file.write(string.getBytes());
file.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and read:
public void readOnFile(){
try {
FileInputStream file = openFileInput(fileName);
if(file!=null){
InputStreamReader inputreader = new InputStreamReader(file);
BufferedReader buffreader = new BufferedReader(inputreader);
String course;
while((course = buffreader.readLine()) != null){
adapter.add(course);
}
}
file.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and here’s bit of my spinner’s code:
courseSpinner = (Spinner) findViewById(R.id.courseSpinner);
adapter = new ArrayAdapter <CharSequence> (this, android.R.layout.simple_spinner_item );
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
readOnFile();
adapter.add("The Country Club");
courseSpinner.setAdapter(adapter);
When I update the Spinner instead of seeing two selections which is “course 1” and “course” 2
I see one selection with the text “course 1course 2” :/
How do I fix this?
Since you are interpreting the strings as separated by newline, you need to write each of the strings to new line in the file.
Change your write code to: