存储和清除阵列位置

时间:2016-01-29 08:22:50

标签: java android arrays checkbox

我在选中时使用数组位置中的存储值复选框,取消选中复选框表示清除特定数组索引值并将另一个复选框值存储在同一位置。

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

2 个答案:

答案 0 :(得分:0)

我认为你的问题在这里:

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--;
        }
    }});

if块中应该有chk4而不是chk1。

答案 1 :(得分:0)

你有可能在最后一个区块中将“m”递增出界限吗?

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--;
    }
}});