Search code examples
javaandroidfilespinneradapter

Adding data to spinner


Hey so I'm building a mini app for uni which basically loads a spinner with pre existing data and the user can also add data to the file through multiple fields. When the user fills in the fields and clicks the button, I add it to the data to its relevant array lists.

Now I confused when I add it to the names array which is the array thats fills the spinner, why it automatically adds to the spinner, without me calling an adapter method such as adapter.add(nametxt.getText().toString()).

public void addinglocations(View view){
    String filename = "au_locations.txt";

//add the edittext fields to the arrays lists
    names.add(nametxt.getText().toString());
    lats.add(Double.parseDouble(lattxt.getText().toString()));
    longs.add(Double.parseDouble(longtxt.getText().toString()));
    timezone.add(tztxt.getText().toString());

//writing to file
    String fileContents = nametxt.getText().toString() + "," + lattxt.getText().toString() + "," + longtxt.getText().toString() + "," +  tztxt.getText().toString() + "\r\n";
    FileOutputStream outputStream;

    try {
        outputStream = openFileOutput(filename, Context.MODE_APPEND);
        outputStream.write(fileContents.getBytes());
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

Solution

  • You don't have to add content to the adapter, just to the spinner. But the last time i used a spinner dynamically i had to notify the spinnerAdapter

    names.add("text");
    spinnerArrayAdapter.notifyDataSetChanged();