Initializing an Android activity by reading a CSV file with image IDs, names, and descriptions The 2019 Stack Overflow Developer Survey Results Are InExtracting Android contact InfoSQLite class to manage recipes for an Android applicationRetaining and passing data between ActivitiesProper way to wait end of a service and singleton designCreating large CSV fileExposing configuration elements to programInteractive Android AppWidget collection to populate activity on clickCatching notifications using Accessibility Service Android appBase activity for handling network state changes in AndroidAndroid audio recording using AudioRecord and ByteBuffer
Why Did Howard Stark Use All The Vibranium They Had On A Prototype Shield?
What is the best strategy for white in this position?
Which Sci-Fi work first showed weapon of galactic-scale mass destruction?
Limit the amount of RAM Mathematica may access?
How to manage monthly salary
Manuscript was "unsubmitted" because the manuscript was deposited in Arxiv Preprints
Why is it "Tumoren" and not "Tumore"?
Monty Hall variation
Output the Arecibo Message
Why do UK politicians seemingly ignore opinion polls on Brexit?
How are circuits which use complex ICs normally simulated?
What function has this graph?
Is "plugging out" electronic devices an American expression?
Feasability of miniature nuclear reactors for humanoid cyborgs
Can't find the latex code for the ⍎ (down tack jot) symbol
"What time...?" or "At what time...?" - what is more grammatically correct?
How to deal with fear of taking dependencies
I see my dog run
Unbreakable Formation vs. Cry of the Carnarium
What is the use of option -o in the useradd command?
A poker game description that does not feel gimmicky
Is three citations per paragraph excessive for undergraduate research paper?
How to make payment on the internet without leaving a money trail?
How to reverse every other sublist of a list?
Initializing an Android activity by reading a CSV file with image IDs, names, and descriptions
The 2019 Stack Overflow Developer Survey Results Are InExtracting Android contact InfoSQLite class to manage recipes for an Android applicationRetaining and passing data between ActivitiesProper way to wait end of a service and singleton designCreating large CSV fileExposing configuration elements to programInteractive Android AppWidget collection to populate activity on clickCatching notifications using Accessibility Service Android appBase activity for handling network state changes in AndroidAndroid audio recording using AudioRecord and ByteBuffer
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
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
$endgroup$
bumped to the homepage by Community♦ 6 mins 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 |
$begingroup$
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
$endgroup$
bumped to the homepage by Community♦ 6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
$begingroup$
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?
$endgroup$
– marklar
Jun 30 '18 at 0:07
add a comment |
$begingroup$
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
$endgroup$
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 '18 at 1:50
200_success
131k17157422
131k17157422
asked Jun 29 '18 at 21:39
marklarmarklar
162
162
bumped to the homepage by Community♦ 6 mins 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♦ 6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
$begingroup$
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?
$endgroup$
– marklar
Jun 30 '18 at 0:07
add a comment |
$begingroup$
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?
$endgroup$
– marklar
Jun 30 '18 at 0:07
$begingroup$
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?
$endgroup$
– marklar
Jun 30 '18 at 0:07
$begingroup$
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?
$endgroup$
– marklar
Jun 30 '18 at 0:07
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
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.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
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.
$endgroup$
add a comment |
$begingroup$
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.
$endgroup$
add a comment |
$begingroup$
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.
$endgroup$
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 '18 at 17:35
A Bravo DevA Bravo Dev
539210
539210
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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
$begingroup$
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?
$endgroup$
– marklar
Jun 30 '18 at 0:07