0

I am developing an android app that uses nostra13 library. First, nostra13 uses a class file that contains array of strings which are the urls of the images to be downloaded.

ex.

public static final String[] IMAGES = new String[]{
    "http://www.ltp.com.ph/SiteImages/Technilink/TL_1Q13_CE.jpg",
    "http://www.ltp.com.ph/SiteImages/Technilink/TL_4Q12_CE.jpg",
    "http://www.ltp.com.ph/SiteImages/Technilink/TL_2Q12_CE.jpg",
    "http://www.ltp.com.ph/SiteImages/Technilink/TL_1Q12_CE.jpg",
    "http://www.ltp.com.ph/SiteImages/Technilink/TL_4Q11_CE.jpg",
    "http://www.ltp.com.ph/SiteImages/Technilink/TL_2Q11_CE.jpg",
    "http://www.ltp.com.ph/SiteImages/Technilink/TL_1Q11_CE.jpg",
    "http://www.ltp.com.ph/SiteImages/Technilink/TL_4Q10_CE.jpg"
};

Is there a way to update this string array online? Like I want to put some file online then the app would download it and update the String[] IMAGES? THANKS!

1 Answers1

0

You could change the value stored in each array position with new data obtained on-line, but you would not be able to change the length of the array because it is declared final.

A better approach is to think of the IMAGES array as the source of data for initializing your working array, which you could store in persistent store (e.g., SharedPreferences or SQLite data base—see the guide topic Storage Options). When you retrieve new data from the web, you can then easily replace or update the stored data. You would only use the IMAGES array to initialize your working array and ignore it after that (unless you added a "reset" function to return to the original data).

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thanks! can i do something like public static final String[] IMAGES = new String[] .... then pass db values to IMAGES? – Kenn de Leon Aug 02 '13 at 02:47
  • @KenndeLeon - When you declare a `final` array, the array object that you assign to it cannot be changed; you can only change the elements stored in the array. If you want to protect the array from inadvertent changes from other code, make it a (non-`final`) private member of a small utility class (probably a singleton would be best) that provides accessors and setters for manipulating the values in a controlled way. The utility class can also be made responsible for interacting with the db, if that fits with the rest of the design, or you can have a bulk change setter method. – Ted Hopp Aug 02 '13 at 03:03