我想在点击按钮时获取阵列上的下一个项目。
数组是这样的:
String[] pickupLinesItems = { "a", "b", "c" };
然后我有这个用于微调器
public void onItemSelected(org.holoeverywhere.widget.AdapterView<?> parent,
View view, int position, long id) {
position = spinner.getSelectedItemPosition();
SpinnerAdapter adap = spinner.getAdapter();
if (adap.equals(pickupLinesAdapter)) {
switch (position) {
case 0:
stopPlaying();
speakMedia = MediaPlayer.create(this, R.raw.a);
break;
case 1:
stopPlaying();
speakMedia = MediaPlayer.create(this, R.raw.b);
break;
case 2:
stopPlaying();
speakMedia = MediaPlayer.create(this, R.raw.c);
break;
}
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
int index = 0;
switch (v.getId()) {
case R.id.next:
// here i want to go on the next item on array
break;
case R.id.back:
// here i wanna go back one item on the array
break;
}
}
我将如何做到这一点,我已经尝试了一切,似乎无法得到它。我试图做一个if语句,但没有。
答案 0 :(得分:3)
需要一个变量来保存声明在相同范围内的pickupLinesItems的当前索引 String [] pickupLinesItems = {“a”,“b”,“c”};
例如:
int pickupLinesItemIndex = 0;
然后你可以这样做:
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.next:
if(pickupLinesItemIndex < pickupLinesItems.length)
{
String pickupLine = pickupLinesItemIndex[++pickupLinesItemIndex];
}
break;
case R.id.back:
if(pickupLinesItemIndex > 0)
{
String pickupLine = pickupLinesItemIndex[--pickupLinesItemIndex];
}
break;
}
}
答案 1 :(得分:0)
假设我的评论是正确的,您可以执行以下操作:
//in your onClick
case R.id.next:
int current = spinner.getSelectedItemPosition();
spinner.setSelection(current++);
break;
你可以在后面做同样的事情。此外,请务必检查边界条件(如果您在c上,请确保转到a并且不要尝试伸出数组的边界)。