[英]Android: How to pass Data from an Adapter to the Main Activity from a OnClickListener
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.
我在音乐应用程序中取得了很多成就,但是现在我使用OnClickListener在listViewAdapter中创建了一个按钮,我想在MainActivity中将一首歌曲添加到favList ArrayList中。 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: 以下是我的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);
}
You should create own OnItemClickListener in activity, set it to adapter and then dispatch onClick event with position to that custom listener. 您应该在活动中创建自己的OnItemClickListener,将其设置为adapter,然后将onClick事件与该自定义侦听器一起定位。
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);
}
}
});
You can just use the reference to the songs
that you pass to the adapter when you create it. 您可以仅在创建适配器时使用传递给适配器的
songs
的引用。 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
. 现在,您的ArrayList将包含您的
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. 另一种更简单的方法是将主Activity中的ArrayList设置为
static
,因此您可以从任何地方引用它,但是,在Android上不建议这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.