安卓:传递数据(群众演员),以片段 [英] Android: Pass data(extras) to a fragment
问题描述
我是新来的Android编程和我有问题,同时传递Parcelable的ArrayList到一个片段。 这是推出的活动(工作好!)其中的 feedlist 是一个parcelable 音乐的ArrayList
I'm new to Android programming and I'm having problems while passing an ArrayList of a Parcelable to a fragment. This is the Activity that is launched(working well!) where feedlist is an ArrayList of a parcelable Music.
Intent in = new Intent(context, ListMusicsActivity.class);
in.putExtra("arrayMusic", feedList);
activity.startActivity(in);
片段活动的onCreate()方法:
The fragment Activity onCreate() method:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymusiclist);
if(savedInstanceState != null)
{
ListMusicFragment frag = new ListMusicFragment();
frag.setArguments(getIntent().getExtras());
}
}
该片段code: 公共类ListMusicFragment扩展SherlockFragment {
The Fragment code: public class ListMusicFragment extends SherlockFragment{
private ArrayList<Music> listMusics = new ArrayList<Music>();
private ListView listMusic;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
listMusics = (ArrayList<Music>) getArguments().getSerializable("arrayMusic");
View view = inflater.inflate(R.layout.musiclistview, container, false);
listMusic = (ListView) view.findViewById(R.id.musicListView);
listMusic.setAdapter(new MusicBaseAdapter(getActivity(), listMusics));
return view;
}
}
我认为这个问题是在该行的 listMusics =(ArrayList中)getArguments()getSerializable(arrayMusic);
最后,这是我的音乐类:
Finally this is my Music class:
公共类音乐实现Parcelable {
public class Music implements Parcelable{
private String url;
private String artist;
private String title;
private String duration;
private String info_url;
private String lyrics;
public Music(String url, String artist, String title,
String duration, String lyrics, String info_url)
{
this.url = url;
this.artist = artist;
this.title = title;
this.duration = duration;
this.lyrics = lyrics;
this.info_url = info_url;
}
public Music(Parcel in)
{
url = ParcelUtils.readString(in);
artist = ParcelUtils.readString(in);
title = ParcelUtils.readString(in);
duration = ParcelUtils.readString(in);
info_url = ParcelUtils.readString(in);
lyrics = ParcelUtils.readString(in);
}
public String getUrl()
{
return url;
}
public String getArtist()
{
return artist;
}
public String getTitle()
{
return title;
}
public String getDuration()
{
return duration;
}
public String getLyrics()
{
return lyrics;
}
public String getInfo()
{
return info_url;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags)
{
ParcelUtils.writeString(dest, url);
ParcelUtils.writeString(dest, artist);
ParcelUtils.writeString(dest, title);
ParcelUtils.writeString(dest, duration);
ParcelUtils.writeString(dest, lyrics);
ParcelUtils.writeString(dest, info_url);
}
public static final Parcelable.Creator<Music> CREATOR =
new Parcelable.Creator<Music>() {
public Music createFromParcel(Parcel in)
{
return new Music(in);
}
public Music[] newArray(int size)
{
return new Music[size];
}
};
}
当我运行此$ C C我得到的是一个显示java.lang.NullPointerException在片段onCreateView()方法问题$。 我想AP preciate很多,如果有人向我指出了正确的方向,看看我失败了。
When I run this code the problem I get is a java.lang.NullPointerException in the Fragment onCreateView() method. I would appreciate a lot if someone pointed me in the right direction to see where I am failing.
修改:问题解决了:我只需要这一行添加到片段活动的onCreate()方法(othwewise的getArguments()将返回null):
EDIT: Problem solved: I just needed to add this line to the fragment Activity onCreate() method(othwewise the getArguments() would return null):
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, frag).commit();
和它添加到片段code:
And add this to the fragment code:
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
Bundle bundle = getArguments();
if(bundle != null)
{
listMusics = bundle.getParcelableArrayList("arrayMusic");
listMusic.setAdapter(new MusicBaseAdapter(getActivity(), listMusics));
}
}
,其中listMusics是Parcelable音乐的ArrayList
, where listMusics is an ArrayList of Parcelable Music.
推荐答案
两件事情。首先,我不认为你添加要正确地传递到片段中的数据。需要传递到片段是什么束,而不是意图。因为如果我想发 INT
价值片段的例子,我将创建一个包,把 INT
成束,然后设置包作为创建片段时所用的参数。
Two things. First I don't think you are adding the data that you want to pass to the fragment correctly. What you need to pass to the fragment is a bundle, not an intent. For example if I wanted send an int
value to a fragment I would create a bundle, put the int
into that bundle, and then set that bundle as an argument to be used when the fragment was created.
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);
二来检索你需要获得发送到片段的参数信息。然后,提取的基础上你确定它的键的值。例如,在您的片段:
Second to retrieve that information you need to get the arguments sent to the fragment. You then extract the value based on the key you identified it with. For example in your fragment:
Bundle bundle = this.getArguments();
if (bundle != null) {
int i = bundle.getInt(key, defaulValue);
}
这取决于你把你得到什么样的变化。另外,默认值通常为空
,但并不需要如此。这取决于如果你设置该参数的默认值。
What you are getting changes depending on what you put. Also the default value is usually null
but does not need to be. It depends on if you set a default value for that argument.
最后,我不认为你可以在 onCreateView
做到这一点。我想你一定找回片段的 onActivityCreated
方法,在此数据。我的理由如下。 onActivityCreated
运行后,基本活动已经完成了自己的的onCreate
方法。如果你把你的信息要检索的包durring您的活动的的onCreate
的方法,它不会在存在你的碎片的 onCreateView $ C之内$ C>。尝试在
内容以后更新你的 onActivityCreated
用这个和的ListView。
Lastly I do not think you can do this in onCreateView
. I think you must retrieve this data within your fragment's onActivityCreated
method. My reasoning is as follows. onActivityCreated
runs after the underlying activity has finished its own onCreate
method. If you are placing the information you wish to retrieve within the bundle durring your activity's onCreate
method, it will not exist during your fragment's onCreateView
. Try using this in onActivityCreated
and just update your ListView
contents later.
这篇关于安卓:传递数据(群众演员),以片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!