0

I am developing an android application which has a Activity with two buttons apartments and banks. When user clicks on apartments it should parse data from xml file and assign to the listview. This xml file is saved in the assets folder and I want to create some shared preferences for this file. I also want to update the shared preferences if my device finds the Internet connectivity.

This is the structure of my XML file:

<?xml version="1.0" encoding="UTF-8"?>
<address_book>
    <apartments>
        <apartment>
            <name>Villa Alvarado</name>
            <street_address>6050 Montezuma Rd San Diego CA 92115</street_address>
            <web_address>http://housing.sdsu.edu/housing/apartments.aspx</web_address>  
            <phone>(619) 594-2747</phone>   
        </apartment>
        <apartment>
            <name>Zuma Apartments</name>
            <street_address>6237 Montezuma Rd San Diego CA 92115</street_address>
            <web_address>http://liveatzuma.com/</web_address>
            <phone>(619) 265-9862</phone>
        </apartment>
    <apartments>
    <banks>
        <bank>
            <name>USE Credit Union</name>
            <street_address>5500 Campanile Drive San Diego CA 92182</street_address>
            <web_address>https://www.usecu.org/home/home</web_address>
            <phone>(866) 873-4968</phone>
        </bank>
        <bank>
            <name>Wells Fargo</name>
            <street_address>4690 63rd St San Diego CA 92115</street_address>
            <web_address>https://www.wellsfargo.com/</web_address>
            <phone>(619) 583-9084</phone>
        </bank>
    </banks>
</address_book>
Jeremy D
  • 4,787
  • 1
  • 31
  • 38
Akshay
  • 833
  • 2
  • 16
  • 34
  • What have you tried so far? Do you have any specific issue? Can you show some of your code? Also, what kind of preference do you want to store? – Jeremy D Sep 30 '14 at 05:27
  • @JeremyD I never worked on shared preference before. I saw some tutorial on TutprialsPoint.com but I was not able to understand it properly – Akshay Sep 30 '14 at 05:31
  • So what do you want to know/store? What is your specific use-case? – Jeremy D Sep 30 '14 at 05:32
  • I have above XML file saved in assets folder of my application. currently my app reads this file from assets folder and assigns it's data to the ListView. I have updated file saved at remote location. now I want to update the local XML file with new data or replace with new file when device finds internet connectivity. – Akshay Sep 30 '14 at 05:36

2 Answers2

1

Why not creating such xml file programatically using SharedPreferences

I don't know why you are saving an xml file in the assets folder while installation of the application. I guess you are not aware of the correct usage of the shared preferences,Let me throw some light on the concepts.

1. Creating SharedPreferences xml SharedPreferences are xml files that are created programatically from your code.For creating such xml file you need to write following code

SharedPreferences sharedpreferences = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);

above statement will create an xml file at /data/data/YOUR_PACKAGE_NAME/shared_prefs/MyPreferences.xml


2.Creating Parameter: Now when you call sharedpreferences.getString(Name, "Butterflow"); this statement will create a parameter in xml file MyPreferences named as Name with a default value of Butterflow.


3.Modifying the value: Later you can modify the value of the parameter name by writing following piece of code

      SharedPreferences.Editor editor = sharedpreferences.edit();
      editor.putString(Name,"Akshay");  //new value of the parameter
      editor.commit(); //will commit your changes

4.Retreiving the value: For retrieving value at later time you can write

    String stringValue = sharedprefernces.getString(Name, ""); //Now the second parameter will be useless and dummy because there is no default value needed

So why not creating such xml file programatically,seems easy to manipulate,isn't it?See here too How to use SharedPreferences in Android to store, fetch and edit values

Community
  • 1
  • 1
nobalG
  • 4,544
  • 3
  • 34
  • 72
  • in my case name could be any name of apartment or bank. then how it will be maintained? Or if you know any other way to make it work then please let me know – Akshay Sep 30 '14 at 06:00
  • SharedPreferences automatically handles these things,give it a try – nobalG Sep 30 '14 at 06:01
  • My listView will show both the apartmetnts or banks at the same time so do I have to create two differrnt keys like editor.putString(Name1, "Villa Alvarado") and editor.putString(Name2, "Zuma Apartments"). Ans Same for banks – Akshay Sep 30 '14 at 06:05
  • In this case you must use only one parameter for both of them – nobalG Sep 30 '14 at 06:06
  • while taking look at SharedPreferences I saw Internal Storage option. What is your opinion, in my case which one is good. – Akshay Sep 30 '14 at 06:09
  • Depends on the requirements of your application,If the xml file you are creating will grow in size in future,the i will recommend using sqlite database,(In your case , i am getting a feeling that moving to database will be a better approach) – nobalG Sep 30 '14 at 06:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62155/discussion-between-akshay-and-butterflow). – Akshay Sep 30 '14 at 06:15
0

I didn't understand your use case at first.

You shouldn't use SharedPreferences for that. Use a database. SQLite should be perfect for this use case.

You would have the following logic:

  1. Parse the XML and write data to your database
  2. Find Internet connectivity
  3. Retrieve your data from your database and update it.

Some links to start: Content Providers in Android and SQLite usage in Android application

Jeremy D
  • 4,787
  • 1
  • 31
  • 38