vendredi 29 janvier 2016

Stored and clear array location

I am using check box using stored value in the array location when select and deselect the check box means to be clear the particular array index value and store the another check box value at the same location.

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

    });
}




Aucun commentaire:

Enregistrer un commentaire