我想从RecyclerView.Adapter类调用ActivityClass.java
中的函数。
下面是我的ActivityClass.java函数:
public void payslipActivityNav() {
Bundle b = new Bundle();
if (b != null) {
Intent intent = new Intent(HomePage.this, PayslipActivity.class);
b.putString("UserName", lblUserName.getText().toString());
b.putString("UserDesignation", lblDesignation.getText().toString());
intent.putExtras(b);
startActivity(intent);
}
}
这是我的RecyclerView.Adapter类中的类:
public class SingleItemRowHolder extends RecyclerView.ViewHolder {
protected TextView tvTitle;
protected ImageView itemImage;
protected TextView lblDescription;
public SingleItemRowHolder(final View view) {
super(view);
this.tvTitle = (TextView) view.findViewById(R.id.tvTitle);
this.itemImage = (ImageView) view.findViewById(R.id.itemImage);
this.lblDescription = (TextView) view.findViewById(R.id.lblDescription);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
strSelectedText = tvTitle.getText().toString();
Toast.makeText(v.getContext(), strSelectedText, Toast.LENGTH_SHORT).show();
switch (strSelectedText){
case "Pay":
((ActivityClass)mContext).payslipActivityNav();
break;
}
}
});
}
}
这是我得到的错误java.lang.ClassCastException: android.app.Application cannot be cast to packageName.ActivityClass
我访问ActivityClass方法有什么问题吗? 或者,方法调用是在内部类内部还是在RecyclerView.Adapter类中的任何其他位置执行。
帮助将不胜感激。
提前致谢!
更新
我用这个来解决它。我想到了一些东西,我尝试了这个并且它有效:
switch (strSelectedText){
case "Pay":
Intent intent = new Intent(mContext.getApplicationContext(), ActivityClass.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
break;
}
谢谢大家。
答案 0 :(得分:0)
试试这个:((YourActivity)context).your_method(); //方法应公开
答案 1 :(得分:0)
public static void payslipActivityNav() {
Bundle b = new Bundle();
if (b != null) {
Intent intent = new Intent(HomePage.this, PayslipActivity.class);
b.putString("UserName", lblUserName.getText().toString());
b.putString("UserDesignation", lblDesignation.getText().toString());
intent.putExtras(b);
startActivity(intent);
}
}
public class SingleItemRowHolder扩展了RecyclerView.ViewHolder {
protected TextView tvTitle;
protected ImageView itemImage;
protected TextView lblDescription;
public SingleItemRowHolder(final View view) {
super(view);
this.tvTitle = (TextView) view.findViewById(R.id.tvTitle);
this.itemImage = (ImageView) view.findViewById(R.id.itemImage);
this.lblDescription = (TextView) view.findViewById(R.id.lblDescription);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
strSelectedText = tvTitle.getText().toString();
Toast.makeText(v.getContext(), strSelectedText, Toast.LENGTH_SHORT).show();
switch (strSelectedText){
case "Pay":
ActivityClass.payslipActivityNav();
break;
}
}
});
}
}
答案 2 :(得分:0)
你必须使用回调接口来做到这一点。这是实现这一目标的安全方式。另外,转换上下文不会使代码可重用。
您应该在适配器内创建一个接口并通过它传递值。
您的适配器代码应如下所示:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private OnItemClickListener onItemClickListener;
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
}
@Override
public int getItemCount() {
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public void setData(List<AccountProvider> accountProviders) {
this.accountProviders = accountProviders;
notifyDataSetChanged();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (onItemClickListener != null)
onItemClickListener.onItemClick(tvTitle.getText().toString());
}
}
}
<强>接口强>
public interface OnItemClickListener {
void onItemClick(String value);
}
在活动:
中public MyActivity implements OnItemClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
MyAdapter adapter = new MyAdapter();
adapter.setOnItemClickListener(this);
yourRecyclerView.setAdapter(adapter);
}
@Override
public void onItemClick(String value){
switch (strSelectedText){
case "Pay":
payslipActivityNav();
break;
}
}
}
如果您需要来自适配器的任何值,您可以通过界面传递它。
希望有所帮助:)