Daniele Daniele - 4 months ago 13
Java Question

How to pass single List Items from one Activity to another and how to adapt them to the Layout

In my MainActivity I have got a

RecyclerView
which is Inflated by an
ArrayList<>
that I have also in my
MainActivity
.

When I tap on an Item in the
RecyclerView
I would like to open another Activity that is Inflated by the same Strings and Images I was using in the
RecyclerView
plus some more Images that have to be put in a PagerView.

The questions are the following:


  • How can I pass a single ListItem to another Activity?

  • How do I create an Adapter to inflate both the PagerView and the TextViews?

  • Is the one I'm suggesting a good enough way to achieve what I'm trying to achieve, or Is there a simplier, more efficient way to do it?



I really appreciate any help you could give me, I know it probably isn't easy to understand what I would like to do but please if something is not clear, don't hesitate to ask.

EDIT:

This is how my
ArrayList
looks like:

chords = new ArrayList<Accordo>();
chords.add(new Accordo(R.drawable.do_maggiore, R.drawable.do_maggiore2, R.drawable.do_maggiore3, "Do maggiore", "Do, Mi, Sol"));
chords.add(new Accordo(R.drawable.do5, R.drawable.do5_2, R.drawable.do5_3, "Do 5", "Na, na, na"));
chords.add(new Accordo(R.drawable.do6, R.drawable.do6_2, R.drawable.do6_3, "Do 6", "Note: na, na, na"));
chords.add(new Accordo(R.drawable.do7, R.drawable.do7_2, R.drawable.do7_3, "do 7", "Note: na, na, na"));
//more items


EDIT 2

this is the xml I need to create the Adapter for:

<RelativeLayout android:id="@+id/parte_sopra"
android:layout_width="wrap_content"
android:layout_height="wrap_content" <TextView android:id="@+id/accordo_view_nome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:layout_marginStart="10dp"
android:textSize="25sp"
android:text="TEXTVIEW"
android:layout_marginTop="10dp"
android:fontFamily="sans-serif-condensed"
android:textStyle="bold"
android:textColor="@color/black" />
<TextView android:id="@+id/accordo_view_note"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:fontFamily="sans-serif-medium"
android:text="TEXTVIEW"
android:textSize="24sp"
android:layout_marginTop="12dp"
android:layout_marginEnd="10dp"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_height="250dp"
android:layout_width="match_parent"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp" />


What kind of Adapter should I use?
PagerAdapter
even though I have two
TextViews
outside the
PagerView
?

Answer

You should use Parcelable to pass the data between Activity.

Example: Set data after click on item (in first activity)

intent.putExtra("DATA_ID", MY_DATA);

Get data (secound activity)

Bundle data = getIntent().getExtras();
MY_DATA myData = (MY_DATA) data.getParcelable("DATA_ID");

More here: https://developer.android.com/reference/android/os/Parcelable.html

You can also use Serializable. Serializable is easier to implement but is slower so I recommend Parcelable

How do I create an Adapter to inflate both the PagerView and the TextViews? - more details please

Comments