Android communication between fragment and baseadapter

我怕爱的太早我们不能终老 提交于 2019-11-29 03:57:29

问题


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");
                }
             }
       }
   }

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/32880248/android-communication-between-fragment-and-baseadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!