Yes, you can.
In the adapter Add a new field:
private Context mContext;
In the adapter constructor, add the following code:
public AdapterName(......,Context context){
In the getView (...) adapter:
Button btn=(Button)convertView.findViewById(yourButtonId); btn.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { if(mContext instanceof YourActivityName){ ((YourActivityName)mContext).yourDesiredMethod(); } } });
replace your own class names in which you see your code, your activity, etc.
If you need to use the same adapter for several operations, follow these steps:
Create interface
public interface IMethodCaller{ void yourDesiredMethod(); }
Inject this interface into the actions required to invoke this method.
Then in Adapter getView () call:
Button btn=(Button)convertView.findViewById(yourButtonId); btn.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { if(mContext instanceof IMethodCaller){ ((IMethodCaller)mContext).yourDesiredMethod(); } } });
You are done. If you need to use this adapter for actions that do not require this call mechanism, the code will not be executed (if the verification failed).
Eldhose M Babu Aug 27 '12 at 13:00 2012-08-27 13:00
source share