如何从适配器完成活动..?

时间:2011-10-31 09:37:34

标签: android android-activity listadapter

我尝试将context个活动传递到适配器,然后我尝试了context.finish();但它给了我一个错误,如The method finish() is undefined for the type Context

10 个答案:

答案 0 :(得分:188)

使用活动输入它。

((Activity)context).finish();

答案 1 :(得分:7)

尝试使用以下代码:

public YourAdapterName(......,Context context){

...

this.myContext=context;
}

在你的适配器中getView()

btn.setOnClickListener(new Button.OnClickListener() {

    @Override
    public void onClick(View v) {
        ((YourActivityName)myContext).yourDesiredMethod();

    }
});

答案 2 :(得分:4)

尝试将您的活动作为activity参数传递,然后您就可以在其上调用finish()。希望这会有所帮助。

答案 3 :(得分:1)

对于Kotlin代码:

(context as Activity).finish()

答案 4 :(得分:0)

我没有用它,但我希望它会起作用。 使用: " this.recreate()"如果你想从活动中重新加载它。

如果要从Adapter重新加载,请使用: "((活动)上下文).recreate()"

答案 5 :(得分:0)

使用上下文和完成活动对您的活动名称进行类型转换

答案 6 :(得分:0)

此代码为((Activity)context).finish();,完整代码为

holder.cardUsers.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent1=new Intent(mcontext,AstroChatPanel.class);
        intent1.putExtra("mobile",userslist.get(position).getMobile());
        intent1.putExtra("name",userslist.get(position).getName());
        intent1.putExtra("type","admin");
        mcontext.startActivity(intent1);
        ((Activity)mcontext).finish();
    }
});

答案 7 :(得分:0)

在适配器中它将起作用

import React from 'react';
import { Text, View } from 'react-native';
import { createDrawerNavigator, createAppContainer } from 'react-navigation';

class HomeScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        <Text style={{ color: 'red', fontSize: 30 ,fontWeight: 'bold'}}>Home!</Text>
      </View>
    );
  }
}

class SettingsScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        <Text style={{ color: 'green' , fontSize: 20 ,fontWeight: 'bold'}}>Settings!</Text>
      </View>
    );
  }
}

const MyDrawerNavigator = createDrawerNavigator({
   Home:{ 
      screen: HomeScreen,
   },
   Settings: {
      screen: SettingsScreen,
   },
 });


export default createAppContainer(MyDrawerNavigator);

答案 8 :(得分:-1)

在自定义适配器中尝试在代码

下面调用完成使用
sender.Save(content, 0, false);

答案 9 :(得分:-2)

关闭活动表单自定义适配器仅在方法

 @Override
  public void onClick(View v) {
    MyApplication.value=mCompany.getCompanyId();
    Intent intent = new Intent(MyApplication.context, VaasetActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   intent.putExtra("ID_COMPANY",mCompany.getCompanyId());
    MyApplication.context.startActivity(intent);
    ((Activity)context).finish();
  }
});