Cypher Studioz
Cypher Studioz

Reputation: 21

Can show the toast but cant startActivity

I am using an adapter class to populate my listview, but each item in my listview has two textviews to whom I wanna setOnClick listener. So I set it in adapter class and it works fine when I try to show the toast.

But the problem is I cant startActivity within OnClickListener. The app crashes. Please help or suggest an alternate way to achieve the same. The activity is already mentioned in Manifest.

This Is My Code:-

public class Adapter_NearMe_TyreWorx extends ArrayAdapter<List_NearMe> implements View.OnClickListener {
    ArrayList<List_NearMe> arraylist;



    private Context context;
    private List<List`enter code here`_NearMe> list;
    public Adapter_NearMe_TyreWorx(Context context, int resource, List<List_NearMe> objects) {
        super(context, resource, objects);
        this.context = context;
        this.List = objects;
        arraylist = new ArrayList<List_NearMe>();
        arraylist.addAll(List);

    }
    TextView Btn_Call;
     String Fac_landmark;
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.canvas_two, parent, false);
        List_NearMe list= list.get(position);

        String Fac_name=list.getName();
         Fac_landmark=list.getLandmark();

        String Fac_gMap=list.getgMap();
        String Fac_contact=list.getContact();

        TextView distance=(TextView)view.findViewById(R.id.fac_distance);
        TextView Fac_Name=(TextView)view.findViewById(R.id.fac_name);
        TextView Fac_Address=(TextView)view.findViewById(R.id.fac_address);
         Btn_Call=(TextView)view.findViewById(R.id.btn_call);
        TextView Btn_Go=(TextView)view.findViewById(R.id.btn_go);

        Btn_Go.setOnClickListener(this);
        return view;
    }



    public void onClick(View v) {
       Toast.makeText(getContext(),"Toast text",LENGTH.SHORT).show(); //working toast code



        Intent intent = new Intent(getContext(), SampleActivity.class);
        getContext().startActivity(intent);

    }
}

Upvotes: 1

Views: 74

Answers (3)

Pankaj Nimgade
Pankaj Nimgade

Reputation: 4549

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

                context.startActivity(new Intent(context.getApplicationContext(), SampleActivity.class));
            }
        });

call the method on the context which you are setting in the constructor of the adapter.

as startActivity() can only be started from the method which in the context of the application, as your adapter is not there you would have to reference the context which you have provided at the constructor of your adapter class

Upvotes: 0

Ravi
Ravi

Reputation: 35569

replace getContext() with context.

Intent intent = new Intent(context, SampleActivity.class);
context.startActivity(intent);

Upvotes: 0

Kapil Rajput
Kapil Rajput

Reputation: 11545

You have to add a Intent flag to pass a Intent from a Non - Activity class Intent.FLAG_ACTIVITY_NEW_TASK adding this flag in intent will work fine try to pass your Intent like this :

Intent intent = new Intent(getContext(), SampleActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(intent);

Or

Intent intent = new Intent(getContext(), SampleActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(intent);

Upvotes: 1

Related Questions