gpt4 book ai didi

java - 根据选择 ArrayIndexOutOfBoundsException 填充 Spinner

转载 作者:行者123 更新时间:2023-11-29 20:26:21 25 4
gpt4 key购买 nike

我使用相同的字符串数组将数据填充到两个微调器中,如下所示:

String[] arrayMonths = { "January", "Feburary", "March", "April", "May",
"June", "July", "August", "September", "October", "November","December" };

adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, arrayMonths);

spinner1.setAdapter(adapter);
spinner2.setAdapter(adapter);

现在,我希望每当我在 spinner1 中进行选择时,就像我选择了 August 然后将 September 显示为 spinner2 中的初始值(以及删除 spinner2 中的所有前几个月

以同样的方式,如果我在 spinner1 中选择了 October,则 spinner2 开始为 November

        spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
spinner1.getItemAtPosition(position).toString(), Toast.LENGTH_LONG)
.show();
}

更新 - 根据@AMY 的建议

    public class MainActivity extends AppCompatActivity {

Spinner spinner1, spinner2;
ArrayAdapter<String> adapter;

String newArr[];

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);

final String[] arrayMonths = { "January", "Feburary", "March", "April", "May",
"June", "July", "August", "September", "October", "November","December" };

newArr = new String[] {"January", "Feburary", "March", "April", "May",
"June", "July", "August", "September", "October", "November","December"};

adapter = new ArrayAdapter<String> (MainActivity.this,
android.R.layout.simple_spinner_item, arrayMonths);

spinner1.setAdapter(adapter);

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
spinner1.getItemAtPosition(position).toString(), Toast.LENGTH_LONG)
.show();

newArr = new String[arrayMonths.length-1];
System.arraycopy(arrayMonths , position, newArr , 0, arrayMonths.length);
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, newArr);
spinner2.setAdapter(adapter);
adapter.notifyDataSetChanged();

}

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

}
});

}
}

获取异常: ArrayIndexOutOfBoundsException

 java.lang.ArrayIndexOutOfBoundsException: src.length=12 srcPos=0 dst.length=11 dstPos=0 length=12

最佳答案

这些行的代码中的问题:

newArr = new String[arrayMonths.length]; 
System.arraycopy(arrayMonths, position, newArr, 0, arrayMonths.length);

您需要将数组大小定义为arrayMonths.length - (selected_position+1)

此外,检查方法arraycopy 的签名:

src : the source array to copy the content.

srcPos : the starting index of the content in src.

dst : the destination array to copy the data into.

dstPos : the starting index for the copied content in dst.

length : the number of elements to be copied.

因此,您需要将要复制的元素的长度更改为arrayMonths.length-selected_position

代码 fragment

int nextPosition = position + 1;
int newSize= arrayMonths.length-nextPosition ;
newArr = new String[newSize];
System.arraycopy(arrayMonths, nextPosition , newArr, 0, newSize);

希望对你有帮助ツ

关于java - 根据选择 ArrayIndexOutOfBoundsException 填充 Spinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32471636/

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