简体   繁体   English

Android:如何将数据从适配器传递到OnClickListener的Main Activity

[英]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.

相关问题 Android ListView:如何使用自定义适配器上的活动onClickListener? - Android ListView: How to use the activity onClickListener from a custom Adapter? 如何在主要活动中创建接口,并通过该接口将数据从适配器传递到主要活动? - How to create a interface in main activity and pass the data from adapter to main activity through the interface? 如何将数据从活动传递到 android 中的回收器视图适配器 - How to pass data from a activity to a recycler view adapter in android 如何将连续数据从活动传递到适配器? - How to pass continuous data from activity to adapter? 如何将数据从活动传递到自定义适配器 - how pass data from activity to custom adapter 如何将多个 EditText 值从 RecyclerView 适配器传递到主 Activity? - How to pass multiple EditText values from RecyclerView adapter to main Activity? 如何将值从自定义列表适配器传递到主活动 - How to pass a value from a custom list adapter to the Main Activity 如何将OnClickListener从Activity传递到片段 - How to pass OnClickListener from Activity to Fragment 如何将数据从适配器发送到Android中的活动中 - How to send data from adapter into activity in Android Android-在Activity类和Adapter类之间传递数据 - Android - pass data between from an Activity class to an Adapter class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM