0

I have created this in an activity:

protected void onClickSunSea(View v) {
    persons = (EditText) findViewById(R.id.txtPerson);
    days = (EditText) findViewById(R.id.txtDays);
    String p = persons.getText().toString();
    String d = days.getText().toString();
    String [] array = getResources().getStringArray(R.array.sunsea);
    Intent myintent = new Intent(MainActivity.this, MainActivity.class);
    myintent.putExtra("PERSONS", p);
    myintent.putExtra("DAYS", d);
    myintent.putExtra("PLACES",array);
    startActivity(myintent);
}

To send to this activity:

String person = getIntent().getStringExtra("PERSONS");
    String day = getIntent().getStringExtra("DAYS");

    TextView txtPerson = (TextView) findViewById(R.id.txtViewPersons);
    txtPerson.setText("Persons travelling: " + person);
    TextView txtDay = (TextView) findViewById(R.id.txtViewDays);
    txtDay.setText("Days of traveling: " + day);

    String[] arrayCityBreak = getIntent().getStringArrayExtra("PLACES");
    ArrayAdapter<String> adapterCityBreak = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayCityBreak);
    ListView myview = (ListView) findViewById(R.id.lstView);
    myview.setAdapter(adapterCityBreak);

I have made these arrays in strings.xml:

<resources>
<string name="app_name">Assignment</string>

<array name="citybreak">
    <item>Barcelona, Spain</item>
    <item>Berlin, Germany</item>
    <item>Copenhagen, Denmark</item>
    <item>Gothenburg, Sweden</item>
    <item>Milan, Italy</item>
</array>

<array name="sunsea">
    <item>Alicante, Spain</item>
    <item>Antalya, Turkey</item>
    <item>Athens, Greece</item>
    <item>Florida, USA</item>
    <item>Gran Canaria, Spain</item>
</array>

<array name="skisnow">
    <item>Kiruna, Sweden</item>
    <item>Trondheim, Norway</item>
    <item>Salzburg, Austria</item>
    <item>Kittila, Finland</item>
    <item>Ivalo, Finland</item>
</array>

I want to insert a subitem with a price inside it, for example: Barcelona, Spain 200 And then get that "200" value in a variable and change it, and then put it back in the array to write it out with the new value + some text before it. Like this: Barcelona, Spain the cost for 2 persons is 400 Can anyone help me with that? Hope you understand my question.

Gil Don
  • 3
  • 1

1 Answers1

0

First of all you should use some kind of Map for this data, so you'll be able to have a pair where city name will be a key and the price will be a value. The bad news is that you can't save Map in the resources. But you can "emulate" it, for example use "|" as a separator:

<array name="citybreak">
 <item>Barcelona, Spain|200</item>
 <item>Berlin, Germany|300</item>
 <item>Copenhagen, Denmark|250</item>
 <item>Gothenburg, Sweden|500</item>
 <item>Milan, Italy|700</item>
</array>

Then you can parse these strings and create a Map from it. Check this answer as an example. When you'll have a Map, you can pass it to your second activity, modify prices there and then send it back with new prices.

Community
  • 1
  • 1
Grimmy
  • 2,041
  • 1
  • 17
  • 26
  • Thanks, I will try that – Gil Don Jan 26 '17 at 18:27
  • I wont make the change permanent, it is just for using the value to calculate it and insert the new value, but not save it, just for showcase, if you understood – Gil Don Jan 26 '17 at 19:25
  • Sure, "can't save Map in the resources" - I mean you can't put it to resources like you've done with String's array – Grimmy Jan 26 '17 at 20:56