6

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 TextView and List of this fragment.

In the middle of processing, this fragment will access a BaseAdapter and inside this BaseAdapter I need to use back process_filter method

Basically here is the structure:

MyFragment.java

public class MyFragment extends Fragment {

   private List<String> filter_list;
   private TextView no_of_filter;

   @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");
   .
   process_filter("string 1", "string 2");
   .
   }

   public void process_filter(String in_preference, String current_value)
   {
       no_of_filter.setText(in_preference);
   }

MyAdapter.java

   class MyAdapter extends BaseAdapter {

       public View getView( final int position, View convertView, ViewGroup   parent)
       {
             holder.checkBox.setOnClickListener( new View.OnClickListener() {
                public void onClick(View v) {
                    //Here I need to access back process_filter from fragment 
                    process_filter ("string 1, string 2");
                }
             }
       }
   }
Bih Cheng
  • 655
  • 1
  • 13
  • 28
  • 1
    does my suggestion not help you? It's the way communication between adapter and fragment should be done. – Marko Oct 02 '15 at 06:09
  • @Marko Yes, I've implemented it and its working. I'm not very familiar with Java Object Oriented Programming but if there is a better way to do this, I would welcome StackOverflow community to leave alternate additional answer. For the spirit of Computer Science :) – Bih Cheng Oct 02 '15 at 06:46
  • As far as I know, using interfaces is the right way to do it. Another option would be to make a static method in your Fragment, but that is bad, really bad. – Marko Oct 02 '15 at 07:41

2 Answers2

12

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
    }
}
Marko
  • 20,385
  • 13
  • 48
  • 64
0

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;
}
brahmy adigopula
  • 617
  • 3
  • 15