I have a recyclerview in which I transfer data from the server using JSON. There is no problem with recyclerview.
The problem is that I can not figure out how to use the same data for the same element when switching to another activity.
Here is the code of my recyclerview adapter
@Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { view=inflater.inflate(R.layout.places_list, parent,false); MyHolder holder=new MyHolder(view); return holder; } @Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { MyHolder myHolder= (MyHolder) holder; Places current=placesList.get(position); myHolder.cardName.setText(current.name); myHolder.cardAbout.setText(current.about); Glide.with(context).load(current.image) .into(myHolder.cardImage); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(context,PlaceActivity.class); context.startActivity(intent); intent.putExtra("places", (Serializable) placesList); intent.putExtra("position", position); } }); } Here is the activity code where I want to transfer data
public class PlaceActivity extends AppCompatActivity implements Serializable { Places p; List<Places> plist; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_place); int position; Bundle extras = getIntent().getExtras(); if (extras != null) { int current = extras.getInt("position"); List<Places> Places = (List<Places>)getIntent().getSerializableExtra("places"); TextView textView = (TextView) findViewById(R.id.textView4); p = Places.get(current); int s = current; Toast.makeText(this, s, Toast.LENGTH_SHORT).show(); } } }
Toast created to test, but nothing happens ...
That's what gives when trying to swap putExtra and startActivity
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.admin.friday, PID: 5731 java.lang.RuntimeException: Parcel: unable to marshal value com.example.admin.friday.Places@f32677d at android.os.Parcel.writeValue(Parcel.java:1397) at android.os.Parcel.writeList(Parcel.java:738) at android.os.Parcel.writeValue(Parcel.java:1344) at android.os.Parcel.writeArrayMapInternal(Parcel.java:665) at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330) at android.os.Bundle.writeToParcel(Bundle.java:1079) at android.os.Parcel.writeBundle(Parcel.java:690) at android.content.Intent.writeToParcel(Intent.java:7793) at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2639) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1507) at android.app.Activity.startActivityForResult(Activity.java:3917) at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67) at android.app.Activity.startActivityForResult(Activity.java:3877) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720) at android.app.Activity.startActivity(Activity.java:4200) at android.app.Activity.startActivity(Activity.java:4168) at com.example.admin.friday.PlaceAdapter$1.onClick(PlaceAdapter.java:79) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
putExtra, thenstartActivityshould help. - woesss