Need expert opinion how should i structure this issue. I have a custom method process_filter that resides in a fragment as it needs to access a private
Create an interface from your adapter to your fragment.
In your adapter create the interface and pass it in your adapter's constructor
class MyAdapter extends BaseAdapter {
public interface IProcessFilter {
void onProcessFilter(String string1, String string2)
}
private IProcessFilter mCallback;
public MyAdapter(Context context, String string1, String string2, IProcessFilter callback) {
mCallback = callback;
}
public View getView( final int position, View convertView, ViewGroup parent)
{
holder.checkBox.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
mCallback.onProcessFilter("string1", "string2");
}
}
}
}
Last thing, implement it in your fragment like this
public class MyFragment extends Fragment implements IProcessFilter {
...
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
no_of_filter = (TextView) view.findViewById(R.id.no_of_filter_tv);
MyAdapter custom_adapter = new MyAdapter(context, "string 1", "string 2", this);
}
@Override
public void onProcessFilter(String string1, String string2) {
// Process the filter
}
}
Sorry For late answer, It may useful for newbies
Note: @Marko answer was perfect but it required a small change while initializing adapter with parameters.
Step 1: Create an listener interface.
Public interface AdapterListener{
Public void myListener();
}
Step 2: Implement your interface in Fragment class.
public class MyFragment extends Fragment implements AdapterListener{
@Override
public void myListener(){
//Do what you want
}
Step 3: Initializing Adapter class inside MyFragment
class .
MyFragmentAdapter myfragmentAdapter=new MyFragmentAdapter(MyFragment.this);
Step 4: Initialize your listener in adapter class.
public class MyFragmentAdapter extends Adapter<Your Code Related>{
private AdapterListener adapterListener;
public MyFragmentAdapter(AdapterListener adapterListener){
this.adapterListener=adapterListener;
}