片段和baseadapter之间的Android通信 [英] Android communication between fragment and baseadapter
问题描述
需要专家意见我应该如何构建这个问题。我有一个自定义方法 process_filter ,它位于片段中,因为它需要访问私有 TextView
和列表
这个片段。
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.
在处理过程中,这个片段将访问 BaseAdapter
并在里面这个 BaseAdapter
我需要使用back process_filter 方法
In the middle of processing, this fragment will access a BaseAdapter
and inside this BaseAdapter
I need to use back process_filter method
基本上这里是结构:
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");
}
}
}
}
推荐答案
创建从适配器到片段的接口。
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
}
}
这篇关于片段和baseadapter之间的Android通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!