gpt4 book ai didi

android - 从字符串数组填充微调器

转载 作者:搜寻专家 更新时间:2023-11-01 07:35:31 25 4
gpt4 key购买 nike

我对此很陌生,我有一个可能很简单的问题。

我有 3 个微调器,我想根据用户在前一个微调器上所做的选择从一个数组中填充每个微调器。

现在我已将其设置为显示带有所选数据的 toast ,但我想设置一个要打开的 Activity ,但那是稍后的事情。现在我需要知道如何从 strings.xml 中的数组填充微调器。

.Java

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

/**
* @author madonk
*
*/
public class Region extends Activity {

Spinner sp1,sp2,sp3;
ArrayAdapter<String> reg_adp,sw_city_adp,sw_lake_charles_adp;
List<String> regions,sw_cities,sw_lake_charles;
int pos;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_region);

regions=new ArrayList<String>();

regions.add("Select a Region");
regions.add("Southwest");

sp1= (Spinner) findViewById(R.id.regions_spinner);
sp2= (Spinner) findViewById(R.id.sw_city_spinner);
sp3= (Spinner) findViewById(R.id.sw_lake_charles_spinner);

reg_adp=new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,regions);
reg_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp1.setAdapter(reg_adp);

sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
pos=arg2;
add();

}

private void add() {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), ""+pos, Toast.LENGTH_SHORT).show();

switch(pos)
{
case 0:
sw_cities= new ArrayList<String>();
sw_cities.add("Select a City");


sw_city_adp=new ArrayAdapter<String>(Region.this,
android.R.layout.simple_dropdown_item_1line,sw_cities);
sw_city_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp2.setAdapter(sw_city_adp);

select();

break;
case 1:
sw_cities= new ArrayList<String>();
sw_cities.add("Select a City");
sw_cities.add("Lake Charles");
sw_cities.add("Iowa");
sw_cities.add("Lake Arthur");

sw_city_adp=new ArrayAdapter<String>(Region.this,
android.R.layout.simple_dropdown_item_1line,sw_cities);
sw_city_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp2.setAdapter(sw_city_adp);

select();

break;
}

}

private void select() {
// TODO Auto-generated method stub

sp2.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelectedregions(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub

}

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub

}

public void onNothingSelectedregions(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});

}

public void onNothingSelectedcities(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});
}

stings.xml

<resources>

<string name="app_name">Louisiana Festivals</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_region">Select a Region</string>
<string-array name="regions_array">
<item >Southwest</item>
</string-array>
<string name="select_a_region">Select a Region</string>
<string-array name="southwest_cities">
<item >Lake Charles</item>
<item >Iowa</item>
</string-array>
<string name="sw_select_a_city">Select a City</string>
<string-array name="lake_charles">
<item>Contraband days Pirate Festival</item>
<item>Other festival to be determined</item>
</string-array>

</resources>

最佳答案

您可以使用 getStringArray() 从 strings.xml 中获取字符串数组.所以你想要的代码 fragment 是:

String[] regionsArray = getResources().getStringArray(R.array.regions_array);
regions = new ArrayList<String>(Arrays.asList(regionsArray));

现在无论您输入到 regions_array 中,都将最终出现在区域 ArrayList 中。

关于android - 从字符串数组填充微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11802775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com