Java 如何通过gridView.setOnItemClickListener()中的intent()传递ArrayList数据

Java 如何通过gridView.setOnItemClickListener()中的intent()传递ArrayList数据,java,android,android-studio,Java,Android,Android Studio,我想知道如何通过gridView.setOnItemClickListener()中的intent()传递ArrayList数据,并在ShowTracksActivity.java中获取它。怎么可能 MainActivity.java ArrayList<Artist>artists = new ArrayList<Artist>(); // Artist 1 String[] artist_title = new String[]{ "Title 1", "Title

我想知道如何通过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);
  }
});
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ???
    }
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 );
   }
});
getIntent().getParcelableArrayListExtra("key");
谢谢您的帮助。

您可以尝试:

在主活动中:

ArrayList<Artist> list = (ArrayList<String>) 
getIntent().getSerializableExtra("list");
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);
intent.putExtra(“列表”,艺术家)

在ShowTrack活动中:

ArrayList<Artist> list = (ArrayList<String>) 
getIntent().getSerializableExtra("list");
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);
ArrayList=(ArrayList)
getIntent().getSerializableExtra(“列表”);

而且
Artist
必须是可序列化的
Serializable

您可以在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);
  }
});
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ???
    }
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 );
   }
});
getIntent().getParcelableArrayListExtra("key");

好的,这是一个想法,当你点击艺术家时,你会在你的点击监听器中得到该单元格的位置,所以现在你必须使用该位置,并将艺术家从该位置的艺术家列表中取出。并通过intent传递,在android中,为了传递用户定义的数据/对象,您需要使其可序列化或可打包。 你可以参考这个

此外,Intent/Bundle类还有用于在活动之间传递此类数据的方法

示例代码:

ArrayList<Artist> list = (ArrayList<String>) 
getIntent().getSerializableExtra("list");
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);
在您的项目中单击侦听器:

ArrayList<Artist> list = (ArrayList<String>) 
getIntent().getSerializableExtra("list");
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);
注意:您必须使您的艺术家类可序列化,以便将其传递给其他活动,如何做到这一点,您可以参考以下内容
,我做了类似的东西,以便传递一个完整的对象,不管它使用的是什么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库依赖项,几乎在所有项目中都存在它

您想将ArrayList作为数据传递还是作为ArrayList的选定项传递?Hi@AseemSharma,我显示一个艺术家列表,当我单击其中一个艺术家时,我想将该艺术家的数据传递给ShowTracksActivity.javaOk,让我为此撰写一个答案,因为注释太短。这似乎是一个好方法,我做了一些调整,但putExtra()显示此错误:无法解析方法“putExtra”(java.lang.String,com.example.android.app.Artist)“您是否已将您的艺术家模型类设置为可序列化或可包裹?我使用了Parcelable,并在此处进行了解释[,但现在我在
artists.add(new Artist(…);
'Artist(android.os.Parcel)中的new Artist上出现此错误。”'在'com.example.android.app.Artist'中拥有私有访问权
事实上它打破了我的ArtistAdapter类,我来自PHP,有些逻辑我还不了解…:-/请检查此项并根据需要进行更改。