Java 数组中项的子项,并将其数据获取到变量,然后更改数组中的项
我在一项活动中创建了以下内容:Java 数组中项的子项,并将其数据获取到变量,然后更改数组中的项,java,android,Java,Android,我在一项活动中创建了以下内容: 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();
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);
}
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);
要发送到此活动,请执行以下操作:
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);
}
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);
String person=getIntent().getStringExtra(“PERSONS”);
字符串day=getIntent().getStringExtra(“天”);
TextView txtPerson=(TextView)findViewById(R.id.txtViewPersons);
txtPerson.setText(“旅行人员:+人”);
TextView txtDay=(TextView)findViewById(R.id.txtViewDays);
setText(“旅行天数:+天”);
字符串[]arrayCityBreak=getIntent().getStringArrayExtra(“位置”);
ArrayAdapter Adapter CityBreak=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,arrayCityBreak);
ListView myview=(ListView)findViewById(R.id.lstView);
setAdapter(adapterCityBreak);
我在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>
分配
西班牙巴塞罗那
德国柏林
丹麦哥本哈根
瑞典哥德堡
意大利米兰
西班牙阿利坎特
土耳其安塔利亚
希腊雅典
美国佛罗里达州
西班牙加那利大酒店
基律纳,瑞典
挪威特隆赫姆
奥地利萨尔茨堡
基蒂拉,芬兰
芬兰伊瓦洛
我想插入一个子项,其中包含一个价格,例如:
西班牙巴塞罗那
200
然后在一个变量中获取“200”值并对其进行更改,然后将其放回数组中,用新值+前面的一些文本将其写出。这样地:
西班牙巴塞罗那
两个人的费用是400英镑
有人能帮我吗?
希望您能理解我的问题。首先,您应该为这些数据使用某种类型的,这样您就可以拥有一对,其中城市名称将是一个键,价格将是一个值。坏消息是您无法在参考资料中保存地图。但您可以“模拟”它,例如使用“|”作为分隔符:
<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>
以回答为例。当您有地图时,您可以将其传递到第二个活动,在那里修改价格,然后将其与新价格一起发送回。谢谢,我会尝试使更改不会永久化,只是为了使用该值计算它并插入新值,而不是保存它,只是为了展示,如果您理解“无法在资源中保存地图”-我的意思是,你不能像处理字符串数组那样把它放到资源中