Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android communication between fragment and baseadapter

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");
                }
             }
       }
   }
like image 723
Bih Cheng Avatar asked Oct 01 '15 05:10

Bih Cheng


People also ask

How to communicate with fragment to adapter in Android?

Pass the instance to fragment to your custom adapter through constructor. the way you are calling onSuccess() inside onClick() method is wrong... It should be called on fragment as it is Fragment's method.

How to share data between two fragments in Android?

To pass data between fragments in the same fragment manager, the listener should be added to the destination fragment with requestKey in order to receive the result produces from another fragment with the same key.

How to pass listener to fragment?

You need to call an extra method every time you want to instantiate the Fragment . You can't guarantee that mListener is set at any time. You may need to pepper your Fragment code with null checks. You need to be careful to make sure the listener remains set after lifecycle events such as screen rotation.

How do I call a fragment from activity in Kotlin?

This example demonstrates how to call an activity method from a fragment in an Android App using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

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
    }
}
like image 177
Marko Avatar answered Sep 18 '22 16:09

Marko