java - Overriding value in resource with SharedPreference -


i'm trying figure out if there easy way achieve i"m doing. have predefined values cities in strings.xml. user option change i"m storing internally sharedpreferences (in essence overriding resource values).

firstly assume 1 of correct ways this. i'd keep implementation simple understand how , let's assume acceptable approach (if not, please provide suggestion on doing - not i'm looking at).

my problem use iterator, list etc. , hence i'm using convoluted code sort this. code follows:

      // our predefined cities our resources file     string [] predefinedcities = res.getstringarray(r.array.predefined_cities);     arraylist<string> predefinedcitiesminusoverridden = new arraylist<string >();      // add values our custom cities onto adapter via sharedpreferences     prefs = getsharedpreferences(my_prefs_name, context.mode_private);     iterator<string> usercities = readcitiesfrompref();       // start of sort operation - show these in alphabetical order     list<string> copyofusercities = new arraylist<string>();     while (usercities.hasnext()){          copyofusercities.add(usercities.next());      }      /* code start override resource value, i.e. remove resource value if        our sharedpreferences contain city. means user has over-ridden values      */      int predefcounter = 0;      for(int i=0; i<predefinedcities.length; i++){ // each city in predefined city          boolean duplicatefound = false;          for(int j=0; j<copyofusercities.size(); j++) { // check if there match in user defined value              if(predefinedcities[i].tostring().equals(copyofusercities.get(j).tostring())){                  duplicatefound = true; // set string found                 break; // no need continue checking further              }          }          // here find out whether retain our predefined city or not         if(!duplicatefound){              predefinedcitiesminusoverridden.add(predefcounter, predefinedcities[i]); // retain new string             predefcounter++; // increment counter our string array          }     }   /* end code remove duplicate values      */      arraylist<string> sortedcities = new arraylist<string>();     sortedcities.addall(copyofusercities);     sortedcities.addall(predefinedcitiesminusoverridden);     collections.sort(sortedcities);     sortedcities.add(0, "no city selected"); // first selection should "no city selected"      //end sort operation      arrayadapter<charsequence> adapter = new arrayadapter(             this,             android.r.layout.simple_spinner_item,             sortedcities); 

is there simpler way instead of using 2 arrays, 2 loops, counter etc?

any appreciated

thanks

you can use 1 set object instead of 2 arraylist objects. have similar operations different behavior set collection type.

a collection contains no duplicate elements. more formally, sets contain no pair of elements e1 , e2 such e1.equals(e2), , @ 1 null element. implied name, interface models mathematical set abstraction.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -