Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch statement Android

I have a problem with my switch statement. When i try to select an item from a list it is executing automaticly the last case of the switch statement. Here is my code:

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);          
            }                  
         }
    });
like image 793
Victor Mihaita Avatar asked Dec 15 '22 00:12

Victor Mihaita


2 Answers

you should break

     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);  
like image 132
Blackbelt Avatar answered Dec 17 '22 13:12

Blackbelt


You just give like this in your program it will give one function only ...

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);             
like image 23
Naveen Kumar Kuppan Avatar answered Dec 17 '22 14:12

Naveen Kumar Kuppan