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) 
  • one
    First putExtra , then startActivity should help. - woesss
  • if I change the startactivity and putExtra, then the application crashes when I go to any element - Alex Stuart
  • The word "crashes" is not very informative - attach the error to the question. - woesss
  • I told you, write to the post. I will make a small application, I'll upload the code here. - YaPV
  • Places this class implements the interface Serializable? - J. Meirlen Nov.

1 answer 1

I did not sign work with JSON. But I think you will not be able to figure it out. Model class:

 public class MyModel { private String title; private String description; private String imageUrl; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getImageUrl() { return imageUrl; } public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; } } 

Model Markup:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp"> <ImageView android:id="@+id/image_iv" android:layout_width="150dp" android:layout_height="150dp" android:layout_alignParentStart="true" android:layout_alignParentTop="true" /> <TextView android:id="@+id/title_txv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toEndOf="@+id/image_iv" android:text="TextView" /> <TextView android:id="@+id/description_txv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/title_txv" android:layout_toEndOf="@+id/image_iv" android:text="TextView" /> </RelativeLayout> 

Class MainActivity

 public class MainActivity extends AppCompatActivity implements RecyclerListener{ private List<MyModel> list = new ArrayList<>(); private MyAdapter myAdapter; private RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Π½Π°Ρ…ΠΎΠ΄ΠΈΠΌ рСсайкл ΠΈ Π΅Π³ΠΎ составныС части ΠΈ Π΄Π΅Π»Π°Π΅ΠΌ с Π½ΠΈΠΌΠΈ тСлодвиТСния recyclerView = (RecyclerView)findViewById(R.id.recyclerView); myAdapter = new MyAdapter(this, this); LinearLayoutManager llm = new LinearLayoutManager(this); recyclerView.setAdapter(myAdapter); recyclerView.setLayoutManager(llm); //Ρ€ΡƒΡ‡ΠΊΠ°ΠΌΠΈ вставил строковыС рСсурсы для ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΌΠΎΠ΄Π΅Π»ΠΈ. //Ρ‚ΡƒΡ‚ Ρ‚Ρ‹ ΡƒΠΆΠ΅ сам вмСсто строк вставляй свой JSON for(int a=0; a<10; a++) { final MyModel myModel = new MyModel(); myModel.setTitle("Title_"+a); myModel.setDescription("Description_"+a); myModel.setImageUrl("https://i.mycdn.me/i?r=ADGc-AHOhhk0xU4_G5aVlnAECGRm3fig_cX1pJkGgx7jKyFEKHwKiL9QuGcq4J-B13c"); list.add(myModel); } myAdapter.updateData(list); } //Ρ€Π΅Π°Π»ΠΈΠ·ΠΎΠ²Ρ‹Π²Π°Π΅ΠΌ ΠΌΠ΅Ρ‚ΠΎΠ΄ интСрфСйса для ΠΏΠ΅Ρ€Π΅Ρ…ΠΎΠ΄Π° Π² // 2 Π°ΠΊΡ‚ΠΈΠ²ΠΈΡ‚ΠΈ с ΠΊΠ»ΡŽΡ‡Π°ΠΌΠΈ модСльки ΠΏΠΎ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ ΠΊΠ»ΠΈΠΊΠ½ΡƒΠ» @Override public void openSecondActivity(final MyModel myModel) { Intent intent = new Intent(this, SecondActivity.class); intent.putExtra("titleIntent", myModel.getTitle()); intent.putExtra("descriptionIntent", myModel.getDescription()); intent.putExtra("imageUrlIntent", myModel.getImageUrl()); startActivity(intent); } } 

Adapter class for recycling

 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { private List<MyModel> list; private Context context; private RecyclerListener recyclerListener; public MyAdapter(Context context, RecyclerListener recyclerListener) { this.context = context; this.recyclerListener = recyclerListener; } public void updateData(List<MyModel> list) { this.list = list; notifyDataSetChanged(); } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_model,parent,false); return new MyViewHolder(itemView); } @Override public void onBindViewHolder(final MyViewHolder holder, final int position) { final MyModel myModel = list.get(position); holder.title.setText(myModel.getTitle()); holder.description.setText(myModel.getDescription()); Glide.with(context).load(myModel.getImageUrl()).into(holder.image); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { recyclerListener.openSecondActivity(myModel); } }); } @Override public int getItemCount() { if (list != null) { return list.size(); } else { return 0; } } class MyViewHolder extends RecyclerView.ViewHolder{ private ImageView image; private TextView title; private TextView description; MyViewHolder(View itemView){ super(itemView); this.image = (ImageView) itemView.findViewById(R.id.image_iv); this.title = (TextView) itemView.findViewById(R.id.title_txv); this.description = (TextView) itemView.findViewById(R.id.description_txv); } } } 

Markup activity_main

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> 

Other activity class

 public class SecondActivity extends AppCompatActivity { private TextView title; private TextView description; private ImageView image; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); //Π½Π°Ρ…ΠΎΠ΄ΠΈΠΌ элСмСнты экрана title = (TextView)findViewById(R.id.title_txv); description = (TextView)findViewById(R.id.description_txv); image = (ImageView) findViewById(R.id.image_iv); //Π±Π΅Ρ€Π΅ΠΌ ΠΊΠ»ΡŽΡ‡ΠΈ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΠΏΠ΅Ρ€Π΅Π΄Π°Π»ΠΈ ΠΈΠ· 1 Π°ΠΊΡ‚ΠΈΠ²ΠΈΡ‚ΠΈ Intent intent = getIntent(); String titleIntent= intent.getStringExtra("titleIntent"); String descriptionIntent= intent.getStringExtra("descriptionIntent"); String imageUrlIntent= intent.getStringExtra("imageUrlIntent"); //вставляСм ΠΊΠ»ΡŽΡ‡ΠΈ Π² Ρ€Π°Π·ΠΌΠ΅Ρ‚ΠΊΡƒ title.setText(titleIntent); description.setText(descriptionIntent); Glide.with(this).load(imageUrlIntent).into(image); } } 

Markup another activation

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/image_iv" android:layout_width="150dp" android:layout_height="150dp" android:layout_alignParentEnd="true" android:layout_alignParentTop="true" /> <TextView android:id="@+id/title_txv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toStartOf="@+id/image_iv" android:text="TextView" /> <TextView android:id="@+id/description_txv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/title_txv" android:layout_toStartOf="@+id/image_iv" android:text="TextView" /> </RelativeLayout> 

Interface:

 public interface RecyclerListener{ void openSecondActivity(final Mymodel mymodel); }