将 Arraylist 从片段传递到它自己的活动 [英] Pass Arraylist from fragment to its own Activity
问题描述
我有一个构建歌曲 ArrayList 的片段,我想将该 ArrayList 传递给 framgment 的 Activity .我知道我可以使用界面,但不知道我该怎么做
I have a fragments that builds a songs ArrayList, i want to pass that ArrayList to the framgment's activity . I know i can use interface, but not sure how i could do it
public class SongsListFragment extends Fragment
{
public interface passArrayList {
public void onArticleSelected(Uri articleUri);
// from android guide i don't know what to do
}
}
这是我目前所拥有的,请告诉我为什么我得到 NullPointer
Here is what i have so far, please let me know why im getting NullPointer
在主活动
@Override
public void onFragmentSetSongs(ArrayList<Song> songs){
songsArrayList = songs;
}
@Override
public void onSongListItemClick(int position) {
musicService.setSong(position);
Song playSong = songsArrayList.get(position);
txtCurrentSongTitle.setText(playSong.getTitle());
txtCurrentSongTitle.requestFocus();
musicService.playSong();
btnPlayPause.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_pause));
if (playbackPaused) {
playbackPaused = false;
}
//setDownloaded();
}
在片段上
OnFragmentInteractionListener songsCallBack;
OnFragmentInteractionListener songsItemClick;
public interface OnFragmentInteractionListener {
public void onFragmentSetSongs(ArrayList<Song> s);
public void onSongListItemClick(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
songsCallBack = (OnFragmentInteractionListener) getActivity();
songsItemClick = (OnFragmentInteractionListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
@Override
public void onDetach() {
super.onDetach();
songsCallBack = null;
songsItemClick= null;
}
listView = (ListView) root.findViewById(R.id.listViewSongs);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
songsItemClick.onSongListItemClick(position);
}
});
关于我完成构建songArrayList的方法,我做到了
on the method where i finish building the songArrayList, i did
songsCallBack.onFragmentSetSongs(songArrayList);
当我点击 ListView 时出错
Error When i Click on ListView
at com..activities.MainActivity.onSongListItemClick(MainActivity.java:107)
at com..fragments.SongsListFragment$1.onItemClick(SongsListFragment.java:194)
at android.widget.AdapterView.performItemClick(AdapterView.java:308)
at android.widget.AbsListView.performItemClick(AbsListView.java:1478)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3480)
错误位置
musicService.setSong(position); //Main Activity (MainActivity.java:107)
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
songsItemClick.onSongListItemClick(position); < -- here
}
推荐答案
更改界面名称,使其以大写字母开头.然后在您的活动中将此行添加到您的活动声明中:public class FragmentActivity extends Activity implements PassArrayList
,覆盖接口方法.并在片段中添加以下内容:
Change name of your interface so that it begins from capital letter. Then in your activity add this line to declaration of your activity: public class FragmentActivity extends Activity implements PassArrayList
, override interface method. And in fragment add following:
PassArrayList mCallback;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mCallback = (PassArrayList) getActivity();
}
@Override
public void onDetach() {
super.onDetach();
mCallback = null;
}
在代码中的某处,当您想将歌曲列表传回活动时,请调用 mCallback
对象上的方法 onArticleSelected()
并传递给此方法你的数组列表.然后这个数组列表将作为您活动中方法 onArticleSelected()
的参数,您可以用它制作任何您喜欢的东西.但是不要忘记在 onDetach()
钩子方法中取消指向 mCallback 的链接以防止上下文泄漏
And somewhere in your code, when you want to pass your list of songs back to the activity call the method onArticleSelected()
on your mCallback
object and pass to this method your arraylist. Then this arraylist will come as an argument to the method onArticleSelected()
in your activity and you could make with it anything that you like. But don't forget to nullify link to mCallback in onDetach()
hook method to prevent context leak
这篇关于将 Arraylist 从片段传递到它自己的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!