首页 > 解决方案 > 单击另一个活动时如何发送自定义arraylist的列表项的数据?

问题描述

我有一个自定义歌曲对象的数组列表,当单击列表视图的特定歌曲项时,我想将歌曲名称、艺术家姓名和专辑封面图像发送到另一个活动(歌曲活动)。我创建了一个 SongActivity 来显示用户选择的歌曲的正在播放屏幕。

(歌曲列表活动)这包含歌曲列表。

 public class SongsListActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.songs_list);
    ArrayList<Song> songs = new ArrayList<Song>();
            songs.add(new Song("Earthquake","Marshmello and TYNAN",R.mipmap.earthquake));
        .....

            final SongAdapter adapter = new SongAdapter(this, songs);


            ListView listView = findViewById(R.id.list);

            listView.setAdapter(adapter);

            ListView listview = (ListView) findViewById(R.id.list);
            listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    Intent intent = new Intent(view.getContext(), SongActivity.class);

                }
            });

        }
    }

(SONG ACTIVITY)当用户点击一个特定的歌曲对象时,这个活动开始

import androidx.appcompat.app.AppCompatActivity;

public class SongActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.song_activity);
        ImageView songImage = findViewById(R.id.songImage);
        TextView songName = findViewById(R.id.songName);
        TextView artistName = findViewById(R.id.artistName);

        Intent intent = getIntent();

        songImage.setImageResource(intent.getIntExtra("image",0));
        songName.setText(intent.getStringExtra("songName"));
        artistName.setText(intent.getStringExtra("artistName"));

    }
}

(歌曲适配器)

public class SongAdapter extends ArrayAdapter {

    public SongAdapter(Activity context, ArrayList<Song> songs) {

        super(context, 0, songs);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Check if the existing view is being reused, otherwise inflate the view
        View listItemView = convertView;
        if(listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        }


        Song currentSong = (Song) getItem(position);

        TextView nameTextView = (TextView) listItemView.findViewById(R.id.song_name);

        nameTextView.setText(currentSong.getSongName());

        TextView artistTextView = (TextView) listItemView.findViewById(R.id.artist_name);

        artistTextView.setText(currentSong.getArtistName());

        ImageView iconView = (ImageView) listItemView.findViewById(R.id.song_icon);

        iconView.setImageResource(currentSong.getImageResourceId());

        return listItemView;
    }

}

标签: javaandroidandroid-activityandroid-listviewonclicklistener

解决方案


像这样对你的模态进行 Paracelize

    public class Student implements Parcelable {

    private Integer rollno;
    private String name;
    private Integer age;


    protected Student(Parcel in) {

        age = in.readInt();
        name  = in.readString();
        rollno = in.readInt();

    }

    public Student(Integer age, String name, Integer rollno) {
        this.age = age;
        this.name = name;
        this.rollno = rollno;
    }


    public static final Creator<Student> CREATOR = new Creator<Student>() {
        @Override
        public Student createFromParcel(Parcel in) {
            return new Student(in);
        }

        @Override
        public Student[] newArray(int size) {
            return new Student[size];
        }
    };


    @Override
    public int describeContents() {
        return 0;
    }

    public Integer getRollno() {
        return rollno;
    }

    public void setRollno(Integer rollno) {
        this.rollno = rollno;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }



    public static Creator<Student> getCREATOR() {
        return CREATOR;
    }


  @Override
    public void writeToParcel(Parcel parcel, int i) {

        parcel.writeInt(age);
        parcel.writeString(name);
        parcel.writeInt(rollno);


    }
}

然后设置并获取使用意图

intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, studentList) 
intents.getParcelableArrayList<Student>(Intent.EXTRA_STREAM)

推荐阅读