我有一个ListFragment和一个自定义适配器.
从适配器我从行中定义的按钮获取onClick事件.在onClick方法中我得到一些id,我想传递给ListFragment做一些事情.
如何从适配器调用ListFragment中的方法showTask?
listfragment
public class TaskListFragment extends ListFragment{ /* ... */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mAdapter = new TaskListAdapter(getActivity(),data); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setListAdapter(mAdapter); } public void showTask(long id) { FragmentTransaction ft = getFragmentManager().beginTransaction(); TaskFragment taskFragment = new TaskFragment(); Bundle args = new Bundle(); args.putLong("id",id); taskFragment.setArguments(args); ft.replace(R.id.fragment_container,taskFragment); ft.commit(); } }
适配器
public class TaskListAdapter extends ArrayAdapter<Task>{ /* ... */ private OnClickListener mOnClickListener = new OnClickListener() { @Override public void onClick(View v) { long id = (Integer) v.getTag(); // how can I call showTask(id) ? } }; }