Java fragment和baseadapter之间的Android通信
需要专家意见我应该如何组织这个问题。我有一个自定义方法process\u filter,它驻留在一个片段中,因为它需要访问这个片段的私有Java fragment和baseadapter之间的Android通信,java,android,android-fragments,baseadapter,Java,Android,Android Fragments,Baseadapter,需要专家意见我应该如何组织这个问题。我有一个自定义方法process\u filter,它驻留在一个片段中,因为它需要访问这个片段的私有TextView和List 在处理过程中,这个片段将访问一个BaseAdapter,在这个BaseAdapter中,我需要使用backprocess\u filter方法 基本结构如下: MyFragment.java public class MyFragment extends Fragment { private List<String>
TextView
和List
在处理过程中,这个片段将访问一个BaseAdapter
,在这个BaseAdapter
中,我需要使用backprocess\u 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);
}
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");
}
}
}
}
创建从适配器到片段的接口 在适配器中创建接口并将其传递给适配器的构造函数
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
}
}
很抱歉回答晚了,这可能对新手有用 注意:@Marko-answer非常完美,但在使用参数初始化适配器时需要做一些小改动 步骤1:创建侦听器界面
Public interface AdapterListener{
Public void myListener();
}
步骤2:在片段类中实现接口
public class MyFragment extends Fragment implements AdapterListener{
@Override
public void myListener(){
//Do what you want
}
步骤3:在MyFragment
类中初始化适配器类
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;
}
公共类MyFragmentAdapter扩展了适配器{
专用适配器列表器适配器列表器;
公共MyFragmentAdapter(AdapterListener AdapterListener){
this.adapterListener=adapterListener;
}
我的建议对您没有帮助吗?这是适配器和片段之间的通信方式。@Marko是的,我已经实现了它及其工作原理。我对Java面向对象编程不是很熟悉,但如果有更好的方法,我欢迎StackOverflow社区留下其他答案。对于sp计算机科学irit:)据我所知,使用接口是正确的方法。另一个选择是在你的片段中创建一个静态方法,但这很糟糕,真的很糟糕。救了我一天。谢谢!什么是Class
?它不是类,接口不是接口。只是一个类型错误。避免它。什么是interface