Initializing an Android activity by reading a CSV file with image IDs, names, and descriptions
up vote
3
down vote
favorite
I am using Java to create an Android app. I have the main activity read a CSV file, create objects, and then save them into a singleton class so the rest of the activities in the app can access them easily.
This is working perfectly fine but I am not sure if there are some other ways to achieve the same result that are considered better practice, or if perhaps my solution could cause issues down the line that I am not aware of.
MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createItemObjects();
}
public void createItemObjects(){
InputStream is = getResources().openRawResource(R.raw.items_csv);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
ArrayList<Item> items = new ArrayList<Item>();
String line = "";
try {
while((line = reader.readLine())!=null) {
String values = line.split(",");
String img = values[0];
int imgID = getResources().getIdentifier(img , "drawable", getPackageName());
items.add(new Item(imgID, values[1], values[2]));
}
} catch (IOException e) {
Log.v("Main Activity", "Error Reading File on Line " + line, e);
e.printStackTrace();
}
ItemListHolder.getInstance().setItemsArrayList(items);
}
So as you can see, when the app launches it reads the CSV file, creates Item objects, creates an ArrayList to store those Item objects, then sends it into the ItemListHolder singleton, so that all the Activities in the app can use ItemListHolder.getInstance().getItemsArrayList() to retrieve the ArrayList of Item. Each Item is constructed with an image ID, name, and description from a line in the CSV file.
This works without any issues, but my question is: Is having your Android app create objects from a CSV file everytime it restarts considered good practice/safe to use/efficient ect, or should you be storing your objects in some other way, such as shared preferences, so that they are actually saved in memory and do not need to be created everytime the app launches and then held inside a class.
I am familiar with Java but am very new to Android so I really have no idea what other ways of saving objects may or may not exist, or what is considered good to use. Is it better to save them to the fileOutputStream? Is it better to save them to sharedPreferences? Is there some other storage method that is better?
java android csv
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
3
down vote
favorite
I am using Java to create an Android app. I have the main activity read a CSV file, create objects, and then save them into a singleton class so the rest of the activities in the app can access them easily.
This is working perfectly fine but I am not sure if there are some other ways to achieve the same result that are considered better practice, or if perhaps my solution could cause issues down the line that I am not aware of.
MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createItemObjects();
}
public void createItemObjects(){
InputStream is = getResources().openRawResource(R.raw.items_csv);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
ArrayList<Item> items = new ArrayList<Item>();
String line = "";
try {
while((line = reader.readLine())!=null) {
String values = line.split(",");
String img = values[0];
int imgID = getResources().getIdentifier(img , "drawable", getPackageName());
items.add(new Item(imgID, values[1], values[2]));
}
} catch (IOException e) {
Log.v("Main Activity", "Error Reading File on Line " + line, e);
e.printStackTrace();
}
ItemListHolder.getInstance().setItemsArrayList(items);
}
So as you can see, when the app launches it reads the CSV file, creates Item objects, creates an ArrayList to store those Item objects, then sends it into the ItemListHolder singleton, so that all the Activities in the app can use ItemListHolder.getInstance().getItemsArrayList() to retrieve the ArrayList of Item. Each Item is constructed with an image ID, name, and description from a line in the CSV file.
This works without any issues, but my question is: Is having your Android app create objects from a CSV file everytime it restarts considered good practice/safe to use/efficient ect, or should you be storing your objects in some other way, such as shared preferences, so that they are actually saved in memory and do not need to be created everytime the app launches and then held inside a class.
I am familiar with Java but am very new to Android so I really have no idea what other ways of saving objects may or may not exist, or what is considered good to use. Is it better to save them to the fileOutputStream? Is it better to save them to sharedPreferences? Is there some other storage method that is better?
java android csv
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Just changes the title so it clearly state what this code accomplishes. For the Item class, the second argument is a string that gets assigned to an instance variable called name, and the third is a string that gets assigned to an instance variable called description. I simply want to know what the preferred way of creating and storing objects are for android. Is it better to save them to the fileOutputStream? Is it better to save them to sharedPreferences? Is there some other storage method that is better?
– marklar
Jun 30 at 0:07
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am using Java to create an Android app. I have the main activity read a CSV file, create objects, and then save them into a singleton class so the rest of the activities in the app can access them easily.
This is working perfectly fine but I am not sure if there are some other ways to achieve the same result that are considered better practice, or if perhaps my solution could cause issues down the line that I am not aware of.
MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createItemObjects();
}
public void createItemObjects(){
InputStream is = getResources().openRawResource(R.raw.items_csv);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
ArrayList<Item> items = new ArrayList<Item>();
String line = "";
try {
while((line = reader.readLine())!=null) {
String values = line.split(",");
String img = values[0];
int imgID = getResources().getIdentifier(img , "drawable", getPackageName());
items.add(new Item(imgID, values[1], values[2]));
}
} catch (IOException e) {
Log.v("Main Activity", "Error Reading File on Line " + line, e);
e.printStackTrace();
}
ItemListHolder.getInstance().setItemsArrayList(items);
}
So as you can see, when the app launches it reads the CSV file, creates Item objects, creates an ArrayList to store those Item objects, then sends it into the ItemListHolder singleton, so that all the Activities in the app can use ItemListHolder.getInstance().getItemsArrayList() to retrieve the ArrayList of Item. Each Item is constructed with an image ID, name, and description from a line in the CSV file.
This works without any issues, but my question is: Is having your Android app create objects from a CSV file everytime it restarts considered good practice/safe to use/efficient ect, or should you be storing your objects in some other way, such as shared preferences, so that they are actually saved in memory and do not need to be created everytime the app launches and then held inside a class.
I am familiar with Java but am very new to Android so I really have no idea what other ways of saving objects may or may not exist, or what is considered good to use. Is it better to save them to the fileOutputStream? Is it better to save them to sharedPreferences? Is there some other storage method that is better?
java android csv
I am using Java to create an Android app. I have the main activity read a CSV file, create objects, and then save them into a singleton class so the rest of the activities in the app can access them easily.
This is working perfectly fine but I am not sure if there are some other ways to achieve the same result that are considered better practice, or if perhaps my solution could cause issues down the line that I am not aware of.
MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createItemObjects();
}
public void createItemObjects(){
InputStream is = getResources().openRawResource(R.raw.items_csv);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
ArrayList<Item> items = new ArrayList<Item>();
String line = "";
try {
while((line = reader.readLine())!=null) {
String values = line.split(",");
String img = values[0];
int imgID = getResources().getIdentifier(img , "drawable", getPackageName());
items.add(new Item(imgID, values[1], values[2]));
}
} catch (IOException e) {
Log.v("Main Activity", "Error Reading File on Line " + line, e);
e.printStackTrace();
}
ItemListHolder.getInstance().setItemsArrayList(items);
}
So as you can see, when the app launches it reads the CSV file, creates Item objects, creates an ArrayList to store those Item objects, then sends it into the ItemListHolder singleton, so that all the Activities in the app can use ItemListHolder.getInstance().getItemsArrayList() to retrieve the ArrayList of Item. Each Item is constructed with an image ID, name, and description from a line in the CSV file.
This works without any issues, but my question is: Is having your Android app create objects from a CSV file everytime it restarts considered good practice/safe to use/efficient ect, or should you be storing your objects in some other way, such as shared preferences, so that they are actually saved in memory and do not need to be created everytime the app launches and then held inside a class.
I am familiar with Java but am very new to Android so I really have no idea what other ways of saving objects may or may not exist, or what is considered good to use. Is it better to save them to the fileOutputStream? Is it better to save them to sharedPreferences? Is there some other storage method that is better?
java android csv
java android csv
edited Jun 30 at 1:50
200_success
127k15149412
127k15149412
asked Jun 29 at 21:39
marklar
162
162
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Just changes the title so it clearly state what this code accomplishes. For the Item class, the second argument is a string that gets assigned to an instance variable called name, and the third is a string that gets assigned to an instance variable called description. I simply want to know what the preferred way of creating and storing objects are for android. Is it better to save them to the fileOutputStream? Is it better to save them to sharedPreferences? Is there some other storage method that is better?
– marklar
Jun 30 at 0:07
add a comment |
Just changes the title so it clearly state what this code accomplishes. For the Item class, the second argument is a string that gets assigned to an instance variable called name, and the third is a string that gets assigned to an instance variable called description. I simply want to know what the preferred way of creating and storing objects are for android. Is it better to save them to the fileOutputStream? Is it better to save them to sharedPreferences? Is there some other storage method that is better?
– marklar
Jun 30 at 0:07
Just changes the title so it clearly state what this code accomplishes. For the Item class, the second argument is a string that gets assigned to an instance variable called name, and the third is a string that gets assigned to an instance variable called description. I simply want to know what the preferred way of creating and storing objects are for android. Is it better to save them to the fileOutputStream? Is it better to save them to sharedPreferences? Is there some other storage method that is better?
– marklar
Jun 30 at 0:07
Just changes the title so it clearly state what this code accomplishes. For the Item class, the second argument is a string that gets assigned to an instance variable called name, and the third is a string that gets assigned to an instance variable called description. I simply want to know what the preferred way of creating and storing objects are for android. Is it better to save them to the fileOutputStream? Is it better to save them to sharedPreferences? Is there some other storage method that is better?
– marklar
Jun 30 at 0:07
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
People from Android have a really comprehensive documentation about it at https://developer.android.com/guide/topics/data/data-storage.
Short answer: yes, there are better ways: SharedPreferences for simple data; Local database (SQLite) for complex data. Both of them can be persistent.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
People from Android have a really comprehensive documentation about it at https://developer.android.com/guide/topics/data/data-storage.
Short answer: yes, there are better ways: SharedPreferences for simple data; Local database (SQLite) for complex data. Both of them can be persistent.
add a comment |
up vote
0
down vote
People from Android have a really comprehensive documentation about it at https://developer.android.com/guide/topics/data/data-storage.
Short answer: yes, there are better ways: SharedPreferences for simple data; Local database (SQLite) for complex data. Both of them can be persistent.
add a comment |
up vote
0
down vote
up vote
0
down vote
People from Android have a really comprehensive documentation about it at https://developer.android.com/guide/topics/data/data-storage.
Short answer: yes, there are better ways: SharedPreferences for simple data; Local database (SQLite) for complex data. Both of them can be persistent.
People from Android have a really comprehensive documentation about it at https://developer.android.com/guide/topics/data/data-storage.
Short answer: yes, there are better ways: SharedPreferences for simple data; Local database (SQLite) for complex data. Both of them can be persistent.
answered Jul 13 at 17:35
A Bravo Dev
539110
539110
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197525%2finitializing-an-android-activity-by-reading-a-csv-file-with-image-ids-names-an%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Just changes the title so it clearly state what this code accomplishes. For the Item class, the second argument is a string that gets assigned to an instance variable called name, and the third is a string that gets assigned to an instance variable called description. I simply want to know what the preferred way of creating and storing objects are for android. Is it better to save them to the fileOutputStream? Is it better to save them to sharedPreferences? Is there some other storage method that is better?
– marklar
Jun 30 at 0:07