1

我有这个问题。我有一个 gridView,当我单击一个项目时,它应该打开一个布局,但是我单击的每个按钮都会打开每个布局,所以如果我按下后退按钮,我需要按下它 11 次。我丢失的旧版本代码没有这个问题。请帮忙!

public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);
    GridView gridview = (GridView) findViewById(R.id.gv);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
             switch(position)
                {
                case 0:               
                    Intent ammissione = (new Intent(List.this, Ammissione.class));             
                    startActivity(ammissione);

                case 1:
                    Intent organizzazione = (new Intent(List.this, Organizzazione.class));             
                    startActivity(organizzazione);

                case 2:
                    Intent notizie = (new Intent(List.this, Notizie.class));             
                    startActivity(notizie);

                case 3:
                    Intent percorsi = (new Intent(List.this, Percorsi.class));             
                    startActivity(percorsi);

                case 4:
                    Intent didattica = (new Intent(List.this, Didattica.class));             
                    startActivity(didattica);

                case 5:
                    Intent regolamenti = (new Intent(List.this, Regolamenti.class));             
                    startActivity(regolamenti);

                case 6:
                    Intent diploma = (new Intent(List.this, Diploma.class));             
                    startActivity(diploma);

                case 7:
                    Intent valutazione = (new Intent(List.this, Valutazione.class));             
                    startActivity(valutazione);

                case 8:
                    Intent perterritorio = (new Intent(List.this, PerTerritorio.class));             
                    startActivity(perterritorio);

                case 9:
                    Intent comunicazioni = (new Intent(List.this, Comunicazioni.class));             
                    startActivity(comunicazioni);

                case 10:
                    Intent europa = (new Intent(List.this, Europa.class));             
                    startActivity(europa);

                case 11: 
                    Intent ricerca = (new Intent(List.this, Ricerca.class));             
                    startActivity(ricerca);
                }


        }
    });
  }
4

1 回答 1

0

breakswitch case.

break在每个之后添加case,像这样......

case 0:               
   Intent ammissione = (new Intent(List.this, Ammissione.class));             
   startActivity(ammissione);
   break;

请参阅Switch 语句

于 2013-08-29T00:49:53.477 回答