1

I have an array adapter and inside it an Imagebutton listener calling an activity.

Above my ListView I have and a input search of a ListView item.

When I search for an item by typing the initial letters and Click on Image Button

viewHolder.discounts.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            Intent intent = new Intent(getContext(), DisplayDiscounts.class);
            intent.putExtra("CustomerId",String.valueOf(cu.getId()));
            ((Customers) getContext()).startActivity(intent);

        }
    });

I get java.lang.ClassCastException: android.app.ContextImpl cannot be cast to "ActivityName"

How I can fix this?

Can I stop the input search when item found?

Mr Teacher
  • 69
  • 1
  • 9
  • Possible duplicate of [Explanation of "ClassCastException" in Java](https://stackoverflow.com/questions/907360/explanation-of-classcastexception-in-java) – Shubham AgaRwal Apr 01 '19 at 07:44

2 Answers2

1

Your cast here is the problematic part:

((Customers) getContext()).startActivity(intent);

just drop the cast and use getContext().startActivity(..) instead.

Nima
  • 6,383
  • 7
  • 46
  • 68
0

You can either pass an Activity reference to your activity or try ((Customers) v.getContext()).startActivity(intent);

egoldx
  • 1,490
  • 1
  • 9
  • 14