如何在Fragment类中从public static void调用非静态public void? 或者是否有另一种方法来调用(listViewHolder.dot.setOnClickListener(new View.OnClickListener()和public void search3())
public void search3() {
Toast.makeText( getActivity(),"search3333333: " ,Toast.LENGTH_SHORT ).show();
}
public static void search4 (Context context1,String text) {
Toast.makeText( context1,text,Toast.LENGTH_LONG ).show();
Tab2 someClass = new Tab2();
someClass.search3();
}
//CustomAdapter
//public class CustomAdapter extends BaseAdapter
listViewHolder.dot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText( context,"" + ff.getText().toString() ,Toast.LENGTH_LONG).show();
Tab2.search4(context,"hi hhhh");
}
});
答案 0 :(得分:0)
如果您想从Fragment
调用Activity
或OnClickListener
方法,则必须在某处存储对Fragment
或Activity
的引用。可能这样的事情对你有用:
public class CustomAdapter extends BaseAdapter {
private final Tab2 tab2;
// other fields
public CustomAdapter(Tab2 tab2, /* other constructor params ... */ ) {
this.tab2 = tab2;
// process other params and other initialization
}
...
@Override
public View getView (int position, View convertView, ViewGroup parent) {
...
listViewHolder.dot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tab2.search3();
}
});
...
}
}
答案 1 :(得分:0)
把公共类CustomAdapter像这样在tab2 Fragment类中扩展BaseAdapter class CustomAdapter扩展BaseAdapter并删除静态
非常感谢所有人