Android:如何将数据从适配器传递到OnClickListener的主活动

时间:2016-10-31 18:45:51

标签: android adapter onclicklistener

我真的被困在这里,我是编程的新手。 我在我的音乐应用程序中取得了很多成果,但现在我在listViewAdapter中使用OnClickListener创建了一个按钮,我想在我的MainActivity中向favList ArrayList添加一个Song。有了这段代码,我就有了我想要的歌曲,但是如何将它放入列表?

这是我的代码:

class SongAdapter extends ArrayAdapter<Song> {

    SongAdapter(Context context, ArrayList<Song> songs) {
        super(context, 0, songs);
    }

    @NonNull
    @Override
    public View getView(final int position, View convertView, @NonNull 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 = getItem(position);

        TextView songNameTextView = (TextView) listItemView.findViewById(R.id.song_name);
        assert currentSong != null;
        songNameTextView.setText(currentSong.getSongName());
        TextView songArtistTextView = (TextView) listItemView.findViewById(R.id.song_artist);
        songArtistTextView.setText(currentSong.getArtist());

        ImageButton addToPlaylistButton = (ImageButton) listItemView.findViewById(R.id.addToPlaylist);
        addToPlaylistButton.setTag(position);
        addToPlaylistButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = (Integer) view.getTag();
                Song song = getItem(position);
                Toast.makeText(view.getContext(), song.getAlbum().toString(), Toast.LENGTH_LONG).show();
            }
        });

        return listItemView;
    } }

以下是我的MainActivity中的部分:

final ArrayList<Song> songs = new ArrayList<>();  final ArrayList<Song> favSongs = new ArrayList<>();

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.song_list);
         //Setup the Listview for the Songs
            SongAdapter adapter = new SongAdapter(this, songs);
            ListView listView = (ListView) findViewById(R.id.list);
            listView.setItemsCanFocus(true);
            listView.setAdapter(adapter); 
            }

2 个答案:

答案 0 :(得分:1)

您应该在activity中创建自己的OnItemClickListener,将其设置为适配器,然后将具有位置的onClick事件调度到该自定义侦听器。

interface OnItemClickListener {
    void onItemClicked(int position);
}

活动:

private OnItemClickListener listener = new OnItemClickListener() {
    public void onItemClicked(int position) {
        Song song = adapter.get(position);
        //do whatever you want with song
    }
}

//creation of adapter
SongAdapter adapter = new SongAdapter(this, songs, listener);
适配器中的

private OnItemClickListener onClickListener;

SongAdapter(Context context, ArrayList<Song> songs, OnItemClickListener onClickListener) {
    super(context, 0, songs);
    this.onClickListener = onClickListener;
}

//in getView
addToPlaylistButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (onClickListener != null) {
                onClickListener.onItemClicked(position);
            }
        }
    });

答案 1 :(得分:0)

您可以在创建时使用对传递给适配器的songs的引用。像这样:

class SongAdapter extends ArrayAdapter<Song> {

    private ArrayList<song> songs;

    SongAdapter(Context context, ArrayList<Song> songs) {
        this.songs = songs
        super(context, 0, songs);
    }

    (snip...)

        @Override
        public void onClick(View view) {
            int position = (Integer) view.getTag();
            Song song = getItem(position);

            songs.add(song);
            (...)
        }
    }); // end of listener

}
}

您的ArrayList现在将包含您的Song

另一种更简单的方法是在主Activity static中创建ArrayList,这样你就可以从任何地方引用它,但是,不建议在Android上使用它。