基于switch案例在android中没有正确调用活动

时间:2015-04-28 11:13:02

标签: android switch-statement

嗨,在下面的代码中,当我点击一般注册报告时,它显示的是最后一个。当我点击第二个活动,然后也显示最后一个活动。

任何人都可以帮我解决这个问题。

的java

public class Reports extends Activity {
    ListView list;
    String[] web = {
        "General Registration Report",
            "IPD Patient Report",
            "OPD Patient Report"

    } ;
    Integer[] imageId = {
            R.drawable.image1,
            R.drawable.image2,
            R.drawable.image3


    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.report);


        CustomList adapter = new
                CustomList(Reports.this, web, imageId);
        list=(ListView)findViewById(R.id.list);
                list.setAdapter(adapter);
                list.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(),General_report.class);
                            startActivity(i);
                        case 1:
                            Intent i1=new Intent(getApplicationContext(),Ipd_report.class);
                            startActivity(i1);
                        case 2:
                            Intent i11=new Intent(getApplicationContext(),Opd_report.class);
                            startActivity(i11); 
                        }

                    }
                });

    }

2 个答案:

答案 0 :(得分:0)

您需要在每个案例后调用break

像这样使用它:

    switch(i){

    case 1: doSomething();
    break;
    case 2: doAnotherthing();
    break;
    }

答案 1 :(得分:0)

  case 0:
  Intent i=new Intent(getApplicationContext(),General_report.class);
  startActivity(i);
  break;

  case 1:
  Intent i1=new Intent(getApplicationContext(),Ipd_report.class);
  startActivity(i1);
  break;

  case 2:
  Intent i11=new Intent(getApplicationContext(),Opd_report.class);
  startActivity(i11); 
  break;

您在break;

之后错过了case:声明