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);
}
}
});
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With