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?










share|improve this question
















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















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?










share|improve this question
















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













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?










share|improve this question















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


















  • 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










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.






share|improve this answer





















    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',
    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








    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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 at 17:35









        A Bravo Dev

        539110




        539110






























            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.





            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.




            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

            Feedback on college project

            Futebolista

            Albești (Vaslui)