Android:如何将数据从适配器传递到OnClickListener的Main Activity [英] Android: How to pass Data from an Adapter to the Main Activity from a OnClickListener
问题描述
我真的被困在这里,我是编程的新手. 我在音乐应用程序中取得了很多成就,但是现在我使用OnClickListener在listViewAdapter中创建了一个按钮,我想在MainActivity中将一首歌曲添加到favList ArrayList中.有了此代码,我便有了所需的歌曲,但是如何将其放入列表?
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?
这是我的代码:
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中的部分:
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);
}
推荐答案
您应该在活动中创建自己的OnItemClickListener,将其设置为适配器,然后将onClick事件及其位置分派到该自定义侦听器.
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);
}
活动中:
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);
}
}
});
这篇关于Android:如何将数据从适配器传递到OnClickListener的Main Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!