切换声明Android

时间:2014-03-13 08:12:38

标签: android switch-statement

我的switch语句有问题。当我尝试从列表中选择一个项目时,它会自动执行switch语句的最后一个案例。这是我的代码:

CustomAdapter.java

@Override
 public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;        
         rowView = inflater.inflate(R.layout.list_field, null);
         holder.tv2=(TextView) rowView.findViewById(R.id.textView2);
         holder.tv=(TextView) rowView.findViewById(R.id.titleView);
         holder.img=(ImageView) rowView.findViewById(R.id.imageView1);

     holder.tv2.setText(description[position]);
     holder.tv.setText(result[position]);
     holder.img.setImageResource(imageId[position]);

     rowView.setOnClickListener(new View.OnClickListener() {

         @Override
         public void onClick(View v) {

            Intent intent = new Intent(context, Lesson.class); 

            switch(position){                                                     
                case 0: intent.putExtra("Title", l1);                   
                case 1: intent.putExtra("Title", l2);                   
                case 2: intent.putExtra("Title", l3);                   
                case 3: intent.putExtra("Title", l4);                   
                case 4: intent.putExtra("Title", l5);                   
                case 5: intent.putExtra("Title", l6);                   
                case 6: intent.putExtra("Title", l7);                   
                case 7: intent.putExtra("Title", l8);                   
                case 8: intent.putExtra("Title", l9);                   
                case 9: intent.putExtra("Title", l10);

                context.startActivity(intent);          
            }                  
         }
    });

2 个答案:

答案 0 :(得分:6)

你应该打破

     switch(position){                                                     
            case 0: intent.putExtra("Title", l1); break;                   
            case 1: intent.putExtra("Title", l2); break;                   
            case 2: intent.putExtra("Title", l3); break;                   
            case 3: intent.putExtra("Title", l4); break;                   
            case 4: intent.putExtra("Title", l5); break;                   
            case 5: intent.putExtra("Title", l6); break;                   
            case 6: intent.putExtra("Title", l7); break;                   
            case 7: intent.putExtra("Title", l8); break;                  
            case 8: intent.putExtra("Title", l9); break;                   
            case 9: intent.putExtra("Title", l10); break;
            default:
                Log.e("", "no case"); 
                return;                        
        }    
          context.startActivity(intent);  

答案 1 :(得分:2)

你只需在程序中给出这样的功能,它只会提供一个功能......

switch(position){                                                     
            case 0: intent.putExtra("Title", l1);    
                    break;             
            case 1: intent.putExtra("Title", l2); 
                    break; 
            case 2: intent.putExtra("Title", l3);
                    break; 
            case 3: intent.putExtra("Title", l4);                   
                    break; 
            case 4: intent.putExtra("Title", l5);                   
                    break; 
            case 5: intent.putExtra("Title", l6);                   
                    break; 
            case 6: intent.putExtra("Title", l7);   
                    break; 
            case 7: intent.putExtra("Title", l8);
                    break; 
            case 8: intent.putExtra("Title", l9);   
                    break; 
            case 9: intent.putExtra("Title", l10);
                    break; 
        }          
 context.startActivity(intent);