将数据从recyclerView适配器发送到新活动时出现问题 [英] Getting problem in sending data from recyclerView adapter to a new activity

查看:151
本文介绍了将数据从recyclerView适配器发送到新活动时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过多次尝试和尝试方法并观察了其他stackoverflow代码之后,我才能够将数据发送到下一个活动. 我想知道:在onBindViewHolder方法中两次调用cursor.moveToPosition是否安全.

After doing many hit and try method and observing other stackoverflow code, I was able to send data to next activity. I want to know: Is it safe to call cursor.moveToPosition twice in onBindViewHolder method.

public class recyclerViewSongAdapter extends RecyclerView.Adapter<recyclerViewSongAdapter.MyViewHolder> {
        private final Activity callingActivity;
        private final Cursor songCursor;

        public recyclerViewSongAdapter(Activity activity, Cursor cursor) {
            callingActivity = activity;
            songCursor = cursor;
            if(songCursor != null) {
                songPathIndex = songCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
                songTitleIndex = songCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
                songAlbumIndex = songCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);
            }
        }

        public class MyViewHolder extends RecyclerView.ViewHolder { //implements View.OnClickListener{
            public TextView titleTextView, albumTextView;
            public View parentLayout;

            public MyViewHolder(View view) {
                super(view);
                titleTextView = view.findViewById(R.id.songTitleId);
                albumTextView = view.findViewById(R.id.songAlbumId);
                parentLayout = view.findViewById(R.id.parentLinearId);

            }
        }

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.song_element, viewGroup, false);
            return new MyViewHolder(itemView);
        }

        public String songTitle, songAlbum;
        int songPathIndex, songTitleIndex, songAlbumIndex;

        @Override
        public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int position) {
            if(songCursor != null) {
                songCursor.moveToPosition(position);
                songTitle = songCursor.getString(songTitleIndex);
                songAlbum = songCursor.getString(songAlbumIndex);

                myViewHolder.titleTextView.setText("" + songTitle);
                myViewHolder.albumTextView.setText("" + songAlbum);

                myViewHolder.parentLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        songCursor.moveToPosition(position);
                        string title = songCursor.getString(songTitleIndex); 
                        Intent intent = new Intent(callingActivity.getApplicationContext(), SongPlaying.class);
                        intent.putExtra("mayank", "" + title);
                        callingActivity.startActivity(intent);
                    }
                });
            }
        }

        @Override
        public int getItemCount() {
            return (songCursor == null) ? 0 : songCursor.getCount();
        }
    }

之前,我的问题是如何将单击项的标题发送到下一个活动,现在我的问题是两次使用cursor.moveToPosition,一个好主意.

推荐答案

这不是在回收站适配器中使用游标的正确方法,但是这是将当前标题发送到下一个活动的解决方案.

This is not a correct way to use cursor in recycler adapter however this is a solution for you to send current title to the next activity.

在您的onclick侦听器中编写以下代码

write below code in your onclick listener

    songCursor.moveToPosition(position);
    String title= songCursor.getString(songTitleIndex);
    Intent intent = new Intent(callingActivity.this,SongPlaying.class);
    intent.putExtra("mayank", "" + title);
    callingActivity.startActivity(intent);

这篇关于将数据从recyclerView适配器发送到新活动时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆