Search code examples
androidcustom-lists

How to use intent in baseadapter class


Hi I have a base adapter class for custom listview. my listview has a button. when i press that button, I have to redirect the control to another activity. When I use Intent to redirect, It shows error during 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 class?


Solution

  • I have solved this problem myself. A simple modification in the Intent has solved it. I had to set flag to my intent. That's 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);
    
    
        }
    }); }