How to use intent in baseadapter class

Hey. I have a base adapter class for a custom list. my list has a button. when I click this button, I need to redirect the control to another action. When I use Intent for redirection, it shows an error at runtime. Here is my code

public View getView(final int position, View convertView, ViewGroup parent) 
{

    convertView = mInflater.inflate(R.layout.listview_elements, null);

    TextView textview1 = (TextView) convertView.findViewById(R.id.TextView01);
    TextView textview2 = (TextView) convertView.findViewById(R.id.TextView02);
    TextView textview3 = (TextView) convertView.findViewById(R.id.TextView03);
    Button buy=(Button)convertView.findViewById(R.id.buy_song_button);
    buy.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

        Intent intent=new Intent(con,MainActivity.class);
        con.startActivity(intent);


        }
    }); }

How to redirect to another activity from my base adapter?

+3
source share
5 answers

I myself have solved this problem. A simple modification in intent solved it. I had to set the flag in my intentions. What is it.

public View getView(final int position, View convertView, ViewGroup parent) 
     {

convertView = mInflater.inflate(R.layout.listview_elements, null);

TextView textview1 = (TextView) convertView.findViewById(R.id.TextView01);
TextView textview2 = (TextView) convertView.findViewById(R.id.TextView02);
TextView textview3 = (TextView) convertView.findViewById(R.id.TextView03);
Button buy=(Button)convertView.findViewById(R.id.buy_song_button);
buy.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

    Intent intent=new Intent(context,MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);


    }
}); }
+15
source

try the following:

  Intent intent=new Intent(activity.getApplicationContext(),MyPlace.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra("city", "Yangon");
    activity.getApplicationContext().startActivity(intent);
+2
source

v.getContext() con onClick

+1

this code worked for me, especially when you have the base adapter for the gridview adapter:

cmnt_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("position",position+"");
                Intent comment_page=new Intent(view.getContext(),comments_page.class);
                comment_page.putExtra("position",position);
                view.getContext().startActivity(comment_page);
            }
+1
source

Pass the context through the constructor, and then just use this line of code:

    Intent intent=new Intent(context.getApplicationContext(), YourActivity.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
      context.getApplicationContext().startActivity(intent);

Thank:)

+1
source

All Articles