我在从共享首选项中检索微调器值时遇到困难。 错误消息是:
java.lang.IndexOutOfBoundsException:索引1无效,大小为1 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.Arrays $ ArrayList.get(Arrays.java:66) 在android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337) 在android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:390) 在android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 在android.widget.Spinner.makeAndAddView(Spinner.java:555) 在android.widget.Spinner.layout(Spinner.java:503) 在android.widget.Spinner.onLayout(Spinner.java:465)
我有两个微调器,第一个微调器用于STATE,第二个微调器用于REPRESENTATIVES。
无论我选择哪种状态,代表微调器都会填充XML文件中包含的arraylist
。
用户选择代表后,按位置保存在sharedPreferences
。
它还会根据保存的位置检索和设置代表的微调器选择。
现在这里出现了问题:
*有些国家的代表人数很少,有些代表很多,例如加利福尼亚州和阿拉斯加州。
如果我选择加利福尼亚州并选择代表号码5,则第二个微调器的位置值将保存为否。 5。
如果我将状态更改为阿拉斯加,则代表的微调器将尝试检索保存在sharedPreferences
上的当前值,但它会崩溃,因为阿拉斯加只有一个代表,其最大值位置为1。
所以,请告诉我该怎么做。我找不到解决方法。
我想要做的是每当我选择一个不同的状态时,它刷新或将第二个微调器(代表)选择值更新为默认值0并将新选择的(代表性)微调器值选择保存到sharedPreferences
。
这是我的活动代码:
public class Location extends AppCompatActivity {
// Wigets - GUI
Spinner statespinner;
Spinner RepresentativeSpinner;
Button btnsubmit;
TextView Senators_Location;
TextView Capital_Location;
TextView Governor_Location;
TextView txtOrigin;
int arrayId;
ArrayAdapter < CharSequence > Rep_Spinner_Adapter;
@
Override
protected void onCreate(final Bundle savedInstanceState {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
// Initialize Spinners
statespinner = (Spinner) findViewById(R.id.State_spinner);
RepresentativeSpinner = (Spinner) findViewById(R.id.Representatives_spinner);
btnsubmit = (Button) findViewById(R.id.state_county_button);
Capital_Location = (TextView) findViewById(R.id.Capital_Location);
Governor_Location = (TextView) findViewById(R.id.Governor_Location);
Senators_Location = (TextView) findViewById(R.id.Senators_Location);
txtOrigin = (TextView) findViewById(R.id.answer_20);
ArrayAdapter < CharSequence > adapter = ArrayAdapter.createFromResource(this, R.array.State_arrays, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
statespinner.setAdapter(adapter);
SharedPreferences sharedPref = getSharedPreferences("FileName", MODE_PRIVATE);
int State_spinnerValue = sharedPref.getInt("userChoiceState", -1);
if (State_spinnerValue != -1)
statespinner.setSelection(State_spinnerValue);
statespinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@
Override
public void onItemSelected(AdapterView <? > adapter, View v, int position, long id) {
SharedPreferences sharedPref = getSharedPreferences("FileName", Context.MODE_PRIVATE);
int state_choice = statespinner.getSelectedItemPosition();
String Senate_Selection = adapter.getItemAtPosition(position).toString();
SharedPreferences.Editor prefEditorState = sharedPref.edit();
prefEditorState.putString("Senators_Choice", Senate_Selection);
prefEditorState.putInt("userChoiceState", state_choice);
prefEditorState.apply();
String State_Item = adapter.getItemAtPosition(position).toString();
if (State_Item.equals("Select A State")) {
Senators_Location.setText(null);
Capital_Location.setText(null);
Governor_Location.setText(null);
arrayId = R.array.Blank_Rep_arrays;}
else if (State_Item.equals("Alabama")) {
Capital_Location.setText(getString(R.string.Alabama_Capital));
Governor_Location.setText(getString(R.string.Alabama_Governor));
Senators_Location.setText(getString(R.string.Alabama_Senators));
arrayId = R.array.Alabama_arrays;}
else if (State_Item.equals("Alaska")) {
Capital_Location.setText(getString(R.string.Alaska_Capital));
Governor_Location.setText(getString(R.string.Alabama_Governor));
Senators_Location.setText(getString(R.string.Alaska_Senators));
arrayId = R.array.Alaska_arrays;}
else if (State_Item.equals("American Samoa")) {
Capital_Location.setText(getString(R.string.American_Samoa_Capital));
Governor_Location.setText(getString(R.string.American_Samoa_Governor));
Senators_Location.setText(getString(R.string.American_Samoa_Senators));
arrayId = R.array.American_Samoa_arrays;}
else if (State_Item.equals("Arizona")) {
Capital_Location.setText(getString(R.string.Arizona_Capital));
Governor_Location.setText(getString(R.string.Arizona_Governor));
Senators_Location.setText(getString(R.string.Arizona_Senators));
arrayId = R.array.Arizona_Representatives_array;}
Rep_Spinner_Adapter =
ArrayAdapter.createFromResource(Location.this, arrayId, android.R.layout.simple_spinner_item);
Rep_Spinner_Adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
RepresentativeSpinner.setAdapter(Rep_Spinner_Adapter);
SharedPreferences sharedPrefRep = getSharedPreferences("FileName", MODE_PRIVATE);
int Rep_spinnerValue = sharedPrefRep.getInt("userChoiceRep", -1);
if (Rep_spinnerValue != -1) RepresentativeSpinner.setSelection(Rep_spinnerValue);
// Showing selected spinner item
Toast.makeText(getApplicationContext(),"Selected State: " + State_Item, Toast.LENGTH_LONG).show();}
@
Override
public void onNothingSelected(AdapterView <? > arg0) {
// TODO Auto-generated method stub
}
});
RepresentativeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@
Override
public void onItemSelected(AdapterView <? > Rep_adapter, View v, int position, long id) {
SharedPreferences sharedPrefRep = getSharedPreferences("FileName", MODE_PRIVATE);
String representatives_item = Rep_adapter.getItemAtPosition(position).toString();
int Rep_choice = RepresentativeSpinner.getSelectedItemPosition();
SharedPreferences.Editor prefEditorRep = sharedPrefRep.edit();
prefEditorRep.putString("RepSelected", representatives_item);
prefEditorRep.putInt("userChoiceRep", Rep_choice);
prefEditorRep.apply();
Toast.makeText(getApplicationContext(),"Selected Representatives: " + representatives_item, Toast.LENGTH_LONG).show();}
@
Override
public void onNothingSelected(AdapterView <? > arg0) {
// TODO Auto-generated method stub
}
});
}
public void onDestroy() {
super.onDestroy();
}
}
这是我的数据代表XML文件:
public class Location extends AppCompatActivity {
// Wigets - GUI
Spinner statespinner;
Spinner RepresentativeSpinner;
Button btnsubmit;
TextView Senators_Location;
TextView Capital_Location;
TextView Governor_Location;
TextView txtOrigin;
int arrayId;
ArrayAdapter < CharSequence > Rep_Spinner_Adapter;
@
Override
protected void onCreate(final Bundle savedInstanceState {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
// Initialize Spinners
statespinner = (Spinner) findViewById(R.id.State_spinner);
RepresentativeSpinner = (Spinner) findViewById(R.id.Representatives_spinner);
btnsubmit = (Button) findViewById(R.id.state_county_button);
Capital_Location = (TextView) findViewById(R.id.Capital_Location);
Governor_Location = (TextView) findViewById(R.id.Governor_Location);
Senators_Location = (TextView) findViewById(R.id.Senators_Location);
txtOrigin = (TextView) findViewById(R.id.answer_20);
ArrayAdapter < CharSequence > adapter = ArrayAdapter.createFromResource(this, R.array.State_arrays, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
statespinner.setAdapter(adapter);
SharedPreferences sharedPref = getSharedPreferences("FileName", MODE_PRIVATE);
int State_spinnerValue = sharedPref.getInt("userChoiceState", -1);
if (State_spinnerValue != -1)
statespinner.setSelection(State_spinnerValue);
statespinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@
Override
public void onItemSelected(AdapterView <? > adapter, View v, int position, long id) {
SharedPreferences sharedPref = getSharedPreferences("FileName", Context.MODE_PRIVATE);
int state_choice = statespinner.getSelectedItemPosition();
String Senate_Selection = adapter.getItemAtPosition(position).toString();
SharedPreferences.Editor prefEditorState = sharedPref.edit();
prefEditorState.putString("Senators_Choice", Senate_Selection);
prefEditorState.putInt("userChoiceState", state_choice);
prefEditorState.apply();
String State_Item = adapter.getItemAtPosition(position).toString();
if (State_Item.equals("Select A State")) {
Senators_Location.setText(null);
Capital_Location.setText(null);
Governor_Location.setText(null);
arrayId = R.array.Blank_Rep_arrays;}
else if (State_Item.equals("Alabama")) {
Capital_Location.setText(getString(R.string.Alabama_Capital));
Governor_Location.setText(getString(R.string.Alabama_Governor));
Senators_Location.setText(getString(R.string.Alabama_Senators));
arrayId = R.array.Alabama_arrays;}
else if (State_Item.equals("Alaska")) {
Capital_Location.setText(getString(R.string.Alaska_Capital));
Governor_Location.setText(getString(R.string.Alabama_Governor));
Senators_Location.setText(getString(R.string.Alaska_Senators));
arrayId = R.array.Alaska_arrays;}
else if (State_Item.equals("American Samoa")) {
Capital_Location.setText(getString(R.string.American_Samoa_Capital));
Governor_Location.setText(getString(R.string.American_Samoa_Governor));
Senators_Location.setText(getString(R.string.American_Samoa_Senators));
arrayId = R.array.American_Samoa_arrays;}
else if (State_Item.equals("Arizona")) {
Capital_Location.setText(getString(R.string.Arizona_Capital));
Governor_Location.setText(getString(R.string.Arizona_Governor));
Senators_Location.setText(getString(R.string.Arizona_Senators));
arrayId = R.array.Arizona_Representatives_array;}
Rep_Spinner_Adapter =
ArrayAdapter.createFromResource(Location.this, arrayId, android.R.layout.simple_spinner_item);
Rep_Spinner_Adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
RepresentativeSpinner.setAdapter(Rep_Spinner_Adapter);
SharedPreferences sharedPrefRep = getSharedPreferences("FileName", MODE_PRIVATE);
int Rep_spinnerValue = sharedPrefRep.getInt("userChoiceRep", -1);
if (Rep_spinnerValue != -1) RepresentativeSpinner.setSelection(Rep_spinnerValue);
// Showing selected spinner item
Toast.makeText(getApplicationContext(),"Selected State: " + State_Item, Toast.LENGTH_LONG).show();}
@
Override
public void onNothingSelected(AdapterView <? > arg0) {
// TODO Auto-generated method stub
}
});
RepresentativeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@
Override
public void onItemSelected(AdapterView <? > Rep_adapter, View v, int position, long id) {
SharedPreferences sharedPrefRep = getSharedPreferences("FileName", MODE_PRIVATE);
String representatives_item = Rep_adapter.getItemAtPosition(position).toString();
int Rep_choice = RepresentativeSpinner.getSelectedItemPosition();
SharedPreferences.Editor prefEditorRep = sharedPrefRep.edit();
prefEditorRep.putString("RepSelected", representatives_item);
prefEditorRep.putInt("userChoiceRep", Rep_choice);
prefEditorRep.apply();
Toast.makeText(getApplicationContext(),"Selected Representatives: " + representatives_item, Toast.LENGTH_LONG).show();}
@
Override
public void onNothingSelected(AdapterView <? > arg0) {
// TODO Auto-generated method stub
}
});
}
public void onDestroy() {
super.onDestroy();
}
}