Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

java - 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");
                }
             }
       }
   }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...