片段和baseadapter之间的Android通信

时间:2015-10-01 05:47:04

标签: java android android-fragments baseadapter

需要专家意见我应该如何构建这个问题。我有一个自定义方法 process_filter ,它位于片段中,因为它需要访问此片段的私有TextViewList

在处理过程中,此片段将访问BaseAdapter并在此BaseAdapter内部我需要使用 process_filter 方法

基本上这里是结构:

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

2 个答案:

答案 0 :(得分:10)

创建从适配器到片段的接口。

在适配器中创建接口并将其传递到适配器的构造函数

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

最后,在你的片段中实现它,就像这样

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

答案 1 :(得分:-1)

很抱歉,对于较晚的答案,这可能对新手有用

注意: @Marko的答案很完美,但是在初始化带有参数的适配器时需要做一些改动。

步骤1:。创建一个侦听器界面。

Public interface AdapterListener{
Public void myListener();
}

步骤2:在Fragment类中实现您的界面。

public class MyFragment extends Fragment implements AdapterListener{

@Override
public void myListener(){
//Do what you want
}  

步骤3:MyFragment类中初始化Adapter类。

MyFragmentAdapter myfragmentAdapter=new MyFragmentAdapter(MyFragment.this);

步骤4:在适配器类中初始化监听器。

 public class MyFragmentAdapter extends Adapter<Your Code Related>{
    private AdapterListener adapterListener;

    public MyFragmentAdapter(AdapterListener adapterListener){
    this.adapterListener=adapterListener;
}