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;








3












$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?










share|improve this question











$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

















3












$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?










share|improve this question











$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













3












3








3





$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?










share|improve this question











$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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • $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










1 Answer
1






active

oldest

votes


















0












$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.






share|improve this answer









$endgroup$













    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
    );



    );













    draft saved

    draft discarded


















    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









    0












    $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.






    share|improve this answer









    $endgroup$

















      0












      $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.






      share|improve this answer









      $endgroup$















        0












        0








        0





        $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.






        share|improve this answer









        $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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 13 '18 at 17:35









        A Bravo DevA Bravo Dev

        539210




        539210



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

            Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

            Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar