我想知道如何通过gridView.setOnItemClickListener()中的intent()传递ArrayList数据,并在ShowTracksActivity.java中获取它。怎么可能?
MainActivity.java
ArrayList<Artist>artists = new ArrayList<Artist>();
// Artist 1
String[] artist_title = new String[]{ "Title 1", "Title 2","Title 3", "Title 4" };
artists.add(new Artist("Artist Name", "Album Name", "img_album", artist_title ));
// Artist 2
//...
ArtistAdapter adapter = new ArtistAdapter(this, artists);
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new GridView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Intent ShowTrackIntent = new Intent(MainActivity.this, ShowTracksActivity.class);
// Here ?
// ShowTrackIntent.putExtra( ??? );
startActivity(ShowTrackIntent);
}
});
ShowTracksActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ???
}
感谢您的帮助。
答案 0 :(得分:1)
您可以在 MainActivity.java
中尝试此操作gridView.setOnItemClickListener(new GridView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Intent ShowTrackIntent = new Intent(MainActivity.this, ShowTracksActivity.class);
ShowTrackIntent.putParcelableArrayListExtra("key", ArrayList<T extends Parcelable> list);
startActivity(ShowTrackIntent );
}
});
并在 ShowTracksActivity.java
中检索它getIntent().getParcelableArrayListExtra("key");
答案 1 :(得分:1)
好的,这就是这个想法,当你点击艺术家时,你会在你的点击监听器中得到那个单元格的位置,所以现在你必须使用那个位置并从那个位置的艺术家列表中取出艺术家。并通过intent传递它,在android中传递用户定义的数据/对象,你需要使它们成为Serializable或Parcelable。 您可以参考此question
此外,Intent / Bundle类具有在活动之间传递此类数据的方法。
示例代码:
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
intent.putExtra(key, value); // Here key would be String and value can be either Parcelable or Serializable
startActivity(intent);
在你的案例中点击监听器:
Artist artist = artists.get(postion);
Intent intent = new Intent(MainActivity.this, ShowTracksActivity.class);
intent.putExtra("selected_artist", artist); // Here key would be String and value can be either Parcelable or Serializable
startActivity(intent);
注意:您必须将您的Artist类序列化,以便将其传递给其他活动,对于如何操作,您可以参考此 Why use parcelable?,How To make class parcelable?和The easy and dirty way
答案 2 :(得分:1)
我做了类似的事情,以便使用Gson传递一个完整的对象
Intent ShowTrackIntent = new Intent(MainActivity.this,ShowTracksActivity.class);
ShowTrackIntent .putExtra("ObjectValue",new Gson().toJson(yourObject));
startActivity(ShowTrackIntent);
在第二个活动中,当您拉动对象时,您只需将其拉出,如下所示
MyClassModel myModel=new Gson().fromJson(getIntent().getStringExtra("ObjectValue"),MyClassModel.class);
您需要导入Powerfull Gson库依赖项,几乎所有项目都存在
答案 3 :(得分:0)
你试试:
在MainActivity中:
intent.putExtra("list", artists);
在ShowTracksActivity中:
ArrayList<Artist> list = (ArrayList<String>)
getIntent().getSerializableExtra("list");
Artist
必须是实施Serializable
答案 4 :(得分:-1)
一种方法是使您的类实现Parcelable
interface:
class Artist implements Parcelable{
private String name;
private String surname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(this.name);
parcel.writeString(this.surname);
}
private static Artist readFromParcel(Parcel in) {
Artist artist = new Artist();
artist.setName(in.readString());
artist.setSurname(in.readString());
return artist;
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Artist createFromParcel(Parcel in) {
return readFromParcel(in);
}
public Artist[] newArray(int size) {
return new Artist[size];
}
};
}
因此您需要覆盖describeContents()
和writeToParcel
方法来实现Parcelable
接口,然后将它们放入Bundle
Bundle extras = new Bundle();
extras.putParcelableArray('some_key', artists);
Intent ShowTrackIntent = new Intent(MainActivity.this, ShowTracksActivity.class);
ShowTrackIntent.butExtras(extras);
startActivity(ShowTrackIntent);