Я использую флажок, используя хранимое значение в местоположении массива, когда выбрано и снимите флажок, чтобы очистить значение индекса конкретного массива и сохранить другое значение флажка в том же месте.Сохраненное и ясное расположение массива
public class MainActivity extends Activity {
int m[] =new int[5];
int count=1;
int t=0;
CheckBox chk1,chk2,chk3,chk4;
int flag,increment=0;
int b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chk1=(CheckBox)findViewById(R.id.checkBox1);
chk2=(CheckBox)findViewById(R.id.checkBox2);
chk3=(CheckBox)findViewById(R.id.checkBox3);
chk4=(CheckBox)findViewById(R.id.checkBox4);
Button next=(Button)findViewById(R.id.next);
Button previous=(Button)findViewById(R.id.previous);
chk1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
// TODO Auto-generated method stub
if(chk1.isChecked())
{
flag=1;
m[increment]=flag;
increment++;
count++;
}
else
{ increment--;
count--;
}
}
});
chk2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
// TODO Auto-generated method stub
if(chk2.isChecked())
{
flag=2;
m[increment]=flag;
increment++;
count++;
}
else
{
increment--;
count--;
}
}
});
chk3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
// TODO Auto-generated method stub
if(chk3.isChecked())
{
flag=3;
m[increment]=flag;
increment++;
count++;
}
else
{
increment--;
count--;
}
}
});
chk4.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
// TODO Auto-generated method stub
if(chk1.isChecked())
{
flag=4;
m[increment]=flag;
increment++;
count++;
}
else
{
increment--;
count--;
}
}});
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t++;
if(t<4)
{
switch (t) {
case 1:
b=m[0];
if(b==1)
{
Toast.makeText(getApplicationContext(), "one", 100).show();
}else if(b==2)
{
Toast.makeText(getApplicationContext(), "two", 100).show();
}else if(b==3)
{
Toast.makeText(getApplicationContext(), "three", 100).show();
}else if(b==4)
{
Toast.makeText(getApplicationContext(), "four", 100).show();
}
break;
case 2:
b=m[1];
if(b==1)
{
Toast.makeText(getApplicationContext(), "one", 100).show();
}else if(b==2)
{
Toast.makeText(getApplicationContext(), "two", 100).show();
}else if(b==3)
{
Toast.makeText(getApplicationContext(), "three", 100).show();
}else if(b==4)
{
Toast.makeText(getApplicationContext(), "four", 100).show();
}
break;
case 3:
b=m[2];
if(b==1)
{
Toast.makeText(getApplicationContext(), "one", 100).show();
}else if(b==2)
{
Toast.makeText(getApplicationContext(), "two", 100).show();
}else if(b==3)
{
Toast.makeText(getApplicationContext(), "three", 100).show();
}else if(b==4)
{
Toast.makeText(getApplicationContext(), "four", 100).show();
}
break;
case 4:
b=m[3];
if(b==1)
{
Toast.makeText(getApplicationContext(), "one", 100).show();
}else if(b==2)
{
Toast.makeText(getApplicationContext(), "two", 100).show();
}else if(b==3)
{
Toast.makeText(getApplicationContext(), "three", 100).show();
}else if(b==4)
{
Toast.makeText(getApplicationContext(), "four", 100).show();
}
break;
default:
if(t<=3)
{
t=3;
}
break;
}
}
}
});
previous.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t--;
if(t>=0)
{
switch (t) {
case 1:
b=m[0];
if(b==1)
{
Toast.makeText(getApplicationContext(), "one", 100).show();
}else if(b==2)
{
Toast.makeText(getApplicationContext(), "two", 100).show();
}else if(b==3)
{
Toast.makeText(getApplicationContext(), "three", 100).show();
}else if(b==4)
{
Toast.makeText(getApplicationContext(), "four", 100).show();
}
break;
case 2:
b=m[1];
if(b==1)
{
Toast.makeText(getApplicationContext(), "one", 100).show();
}else if(b==2)
{
Toast.makeText(getApplicationContext(), "two", 100).show();
}else if(b==3)
{
Toast.makeText(getApplicationContext(), "three", 100).show();
}else if(b==4)
{
Toast.makeText(getApplicationContext(), "four", 100).show();
}
break;
case 3:
b=m[2];
if(b==1)
{
Toast.makeText(getApplicationContext(), "one", 100).show();
}else if(b==2)
{
Toast.makeText(getApplicationContext(), "two", 100).show();
}else if(b==3)
{
Toast.makeText(getApplicationContext(), "three", 100).show();
}else if(b==4)
{
Toast.makeText(getApplicationContext(), "four", 100).show();
}
break;
case 4:
b=m[3];
if(b==1)
{
Toast.makeText(getApplicationContext(), "one", 100).show();
}else if(b==2)
{
Toast.makeText(getApplicationContext(), "two", 100).show();
}else if(b==3)
{
Toast.makeText(getApplicationContext(), "three", 100).show();
}else if(b==4)
{
Toast.makeText(getApplicationContext(), "four", 100).show();
}
break;
default:
if(t<=0)
{
t=1;
}
break;
}
}
}
});
}
Logcat здесь
E/AndroidRuntime(1522): FATAL EXCEPTION: main
E/AndroidRuntime(1522): java.lang.ArrayIndexOutOfBoundsException: length=5index=-1
В чем проблема? –
java.lang.ArrayIndexOutOfBoundsException: length = 5; index = -1 – Rakesh
Установлены ли флажки по умолчанию? –