如何在baseadapter类中使用intent

And*_*lva 3 android custom-lists

嗨我有自定义列表视图的基本适配器类.我的listview有一个按钮.当我按下该按钮时,我必须将控件重定向到另一个活动.当我使用Intent重定向时,它在运行时显示错误.这是我的代码,

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);


        }
    }); }
Run Code Online (Sandbox Code Playgroud)

如何从我的基础适配器类重定向到另一个活动?

And*_*lva 15

我自己解决了这个问题.Intent中的一个简单修改解决了它.我不得不为我的意图设置旗帜.而已.

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);


    }
}); }
Run Code Online (Sandbox Code Playgroud)