I'm creating an Android application in Java using Android Studio and the OpenCSV package. It seems to work fine when I upload the csv file to my device or emulator's file system and access it that way, but for the purposes of the app it should be downloaded with the app itself. I've tried to place it in the assets folder and access it that way, but for whatever reason it cannot find the file to read its information to put in the spinner.
I know there's much less complicated ways to fill a spinner than a csv, it just contains information related to each of those spinner options to display when an item is selected. Am I taking the wrong approach to storing the csv file within the application? I feel like a csv file is my best approach given the data for this application has been created in a large excel file.
Here's a snippet of me trying to access the csv file.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView1);
// create a list of items for the spinner.
String csvfileString = getFilesDir() + "/ShelterInfo.csv";
System.out.println(csvfileString);
File csvfile = new File(csvfileString);
Spinner dropdown = findViewById(R.id.spinner1);
ArrayAdapter<String> spinnerArrayAdapter;
CSVReader reader;
try {
reader = new CSVReader(new FileReader(csvfile));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
I'm creating an Android application in Java using Android Studio and the OpenCSV package. It seems to work fine when I upload the csv file to my device or emulator's file system and access it that way, but for the purposes of the app it should be downloaded with the app itself. I've tried to place it in the assets folder and access it that way, but for whatever reason it cannot find the file to read its information to put in the spinner.
I know there's much less complicated ways to fill a spinner than a csv, it just contains information related to each of those spinner options to display when an item is selected. Am I taking the wrong approach to storing the csv file within the application? I feel like a csv file is my best approach given the data for this application has been created in a large excel file.
Here's a snippet of me trying to access the csv file.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView1);
// create a list of items for the spinner.
String csvfileString = getFilesDir() + "/ShelterInfo.csv";
System.out.println(csvfileString);
File csvfile = new File(csvfileString);
Spinner dropdown = findViewById(R.id.spinner1);
ArrayAdapter<String> spinnerArrayAdapter;
CSVReader reader;
try {
reader = new CSVReader(new FileReader(csvfile));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
Share
Improve this question
asked Mar 29 at 17:54
Alex BurnettAlex Burnett
111 bronze badge
5
- Why Java? Kotlin is preferred: developer.android/kotlin – Ken Wolf Commented Mar 29 at 18:33
- 1 The title is misleading. You know how to read CSV files. You are having an issue doing it. Describe the issue. – aled Commented Mar 30 at 3:01
- Your title does need improvement. Rewrite to summarize your specific technical issue. – Basil Bourque Commented Mar 30 at 4:41
- "display when an item is selected" — Perhaps you should be using localization features. I do not do Android development, so I do not know for sure, but surely the Android platform must provide an internationalization/localization facility for loading such UI textual items. – Basil Bourque Commented Mar 30 at 4:43
- Please adjust title as is not related to description – dawid debinski Commented Mar 31 at 9:00
1 Answer
Reset to default 0The approach you're taking (using a CSV file to store and retrieve data for your Spinner) is perfectly valid, especially given that your data originates from a large Excel file.
The problem here is the way you're trying to access the file. When you use getFilesDir()
, you're pointing to the app's internal storage directory, but assets files aren't stored there. Assets are packaged with your app in a different location.
Use getAssets().open("ShelterInfo.csv"). This ensures the file is ready correctly from the assets folder. Create an InputStreamReader from the opened InputStream. Properly catch the potential IOException.
I hope the following code snippet helps - I haven't written Java in some time!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView1);
Spinner dropdown = findViewById(R.id.spinner1);
List<String> spinnerItems = new ArrayList<>();
try {
// Open the CSV file from assets folder
InputStream inputStream = getAssets().open("ShelterInfo.csv");
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
// Create the CSV reader
CSVReader reader = new CSVReader(inputStreamReader);
// Read CSV data
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
// Assuming the first column contains spinner items
spinnerItems.add(nextLine[0]);
}
// Create and set adapter
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(
this, android.R.layout.simple_spinner_dropdown_item, spinnerItems);
dropdown.setAdapter(spinnerArrayAdapter);
reader.close();
} catch (IOException e) {
e.printStackTrace();
textView.setText("Error reading CSV: " + e.getMessage());
}
}