我试图从我的Recycler视图适配器调用一个activity方法。
我做了什么,
我有创建了一个界面,实现了所需的活动覆盖其方法并尝试从适配器调用
我收到java.lang.ClassCastException:android.app.Application无法强制转换为my.package.Interface.InterfaceName
下面是代码
MethodCaller.java
public interface MethodCaller {
void deleteAddress(String did);
}
AddressAdapter.java
public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.ViewHolder> {
private Context context;
private List<Address> list;
public AddressAdapter(Context context, List<Address> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public AddressAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.address_single, parent, false);
return new AddressAdapter.ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull AddressAdapter.ViewHolder holder, final int position) {
Address address = list.get(position);
holder.btn_deleteAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Address address = list.get(position);
final String id = address.getId();
//if(context instanceof MethodCaller){
((MethodCaller)context).deleteAddress(id); //ClassCastException - Error
//}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public Button btn_deleteAddress;
public ViewHolder(View itemView) {
super(itemView);
btn_deleteAddress = itemView.findViewById(R.id.btn_deleteAddress);
}
}
}
AddressActivity.java
public class AddressActivity extends AppCompatActivity implements MethodCaller {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_address);
//CODE
}
}
@Override
public void deleteAddress(String did) {
Toast.makeText(this, did, Toast.LENGTH_SHORT).show();
/*DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
deleteAddressRequest(id);
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure you want to delete this item?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();*/
}
}
答案 0 :(得分:2)
试试这个
public MethodCaller methodCaller;
public AddressAdapter(Context context, List<Address> list,MethodCaller methodCaller) {
this.context = context;
this.list = list;
this.methodCaller = methodCallerl;
}
@Override
public void onBindViewHolder(@NonNull AddressAdapter.ViewHolder holder, final int position) {
Address address = list.get(position);
holder.btn_deleteAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Address address = list.get(position);
final String id = address.getId();
methodCaller .deleteAddress(id); //ClassCastException - Error
}
});
}
<强> AddressActivity.java 强>
像这样在你的适配器中传递接口;
new AddressAdapter(context,list,this);
答案 1 :(得分:1)
您可以使用传递给适配器的活动上下文从适配器调用活动方法
((YourActivityName)mContext).customMethod();
如果您将同一适配器用于多个类别,则可以在执行操作之前添加检查。
if(mContext instanceof YourActivityName){
((YourActivityName)mContext).yourActivityMethod();
}
有关更多详细信息,请遵循此https://trinitytuts.com/tips/call-activity-method-inside-adapter/