how to pass Position wise object data one activity to another using recyclerview and databse in android

。_饼干妹妹 提交于 2019-12-08 19:42:32

Add Intent in below Method,

    holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                build.setSelected(!build.isSelected());
                if (build.isSelected()) {
                    holder.button.setBackgroundResource(R.drawable.selected_true_icon_new);
    Intent intent = new Intent(context, youractivity.class)
    intenet.putExtra("key","value");
    context.startActivity(intent);
                } else
                    holder.button.setBackgroundResource(R.drawable.add_button_icon);
            }
        });

First you should implement Serializable interface in your Build model class like this :-

public class Build implements Serializable{
   //Content will be as it is
}

Change your clickListener like this :-

holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                build.setSelected(!build.isSelected());
                if (build.isSelected()) {
                    holder.button.setBackgroundResource(R.drawable.selected_true_icon_new);
    Intent intent = new Intent(context, youractivity.class)
    intenet.putExtra("build",build);
    context.startActivity(intent);
                } else
                    holder.button.setBackgroundResource(R.drawable.add_button_icon);
            }
        });

In the onCreate method of receiving activity write this :-

Build build = (Build) getIntent().getSerializableExtra("build");

Huge mistak that you register to the click event on your ViewHolder! you will get diffrent position from the actual because when android use notifyItemMoved the viewBindHolder will not be called and than you got the wrong position.

and in the click listener implementation you should pass Intent with your data

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