简体   繁体   中英

switch function executing several cases

Hi I have been facing a problem since a while and I can't seem to find a solution. I have a gridview with three items, and every one opens an activity when selected. The problem comes when I select the case 0 it first opens the intent i then the intent ii and ends up with opening the intent iii .

If I select case 1 , I open first intent ii and then intent iii . Finally if I select case 2 I open intent iii . THis is the main code!

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mItems = new ArrayList<GridViewItem>();
        Resources resources = getResources();

        mItems.add(new GridViewItem(resources.getDrawable(R.drawable.truck_package), getString(R.string.drop_package)));
        mItems.add(new GridViewItem(resources.getDrawable(R.drawable.man_package), getString(R.string.pick_package)));
        mItems.add(new GridViewItem(resources.getDrawable(R.drawable.register), getString(R.string.register)));
        mAdapter = new GridViewAdapter(this, mItems);
        // initialize the GridView
        final GridView gridView = (GridView) this.findViewById(R.id.gridView);
        gridView.setAdapter(mAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0 :
                        Intent i = new Intent(getApplicationContext(), DropPackage.class);
                        Toast.makeText(getApplicationContext(), "DropPack", Toast.LENGTH_SHORT).show();
                        startActivity(i);

                    case 1 :
                        Intent ii = new Intent(getApplicationContext(), PickPackage.class);
                        Toast.makeText(getApplicationContext(), "PickPack", Toast.LENGTH_SHORT).show();
                        startActivity(ii);
                    case 2 :
                        Intent iii = new Intent(getApplicationContext(), Register.class);
                        Toast.makeText(getApplicationContext(), "Register", Toast.LENGTH_SHORT).show();
                        startActivity(iii);
                }



            }
        });
    }

Any idea of what could be the problem? Thank you :)

You did not put break at the end of case statements. Try adding break at the end of every case statement.

Try this way

switch (position){
    case 0 :
        Intent i = new Intent(getApplicationContext(), DropPackage.class);
        Toast.makeText(getApplicationContext(), "DropPack", Toast.LENGTH_SHORT).show();
        startActivity(i);
        break;

    case 1 :
        Intent ii = new Intent(getApplicationContext(), PickPackage.class);
        Toast.makeText(getApplicationContext(), "PickPack", Toast.LENGTH_SHORT).show();
        startActivity(ii);
        break;

    case 2 :
        Intent iii = new Intent(getApplicationContext(), Register.class);
        Toast.makeText(getApplicationContext(), "Register", Toast.LENGTH_SHORT).show();
        startActivity(iii);
        break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM