Android: How to pass Data from an Adapter to the Main Activity from a OnClickListener

╄→гoц情女王★ 提交于 2019-12-12 05:17:45

问题


I am really stuck here, I am new to Programming. I achieved a lot in my Music App, but now I made a button in my listViewAdapter with a OnClickListener and I want to add a Song to the favList ArrayList in my MainActivity. With this code I have the Song I want, but how do I get it into the List?

Here is my code for this:

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;
    } }

And here are the parts from my 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); 
            }

回答1:


You should create own OnItemClickListener in activity, set it to adapter and then dispatch onClick event with position to that custom listener.

interface OnItemClickListener {
    void onItemClicked(int position);
}

in activity:

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);

in adapter:

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);
            }
        }
    });



回答2:


You can just use the reference to the songs that you pass to the adapter when you create it. Something like this:

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

}
}

And your ArrayList will now contain your Song.

Another, simpler way to do this would be to make the ArrayList in the main Activity static, so you can refer to it from anywhere, however, this is not recommended on Android.



来源:https://stackoverflow.com/questions/40348565/android-how-to-pass-data-from-an-adapter-to-the-main-activity-from-a-onclicklis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!