Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Lukas Baumgartner
Lukas Baumgartner
14,817 Points

Best way to build/store items

Hey!

In my app I'm making, the user can tap on a floating action button to open a dialogue to give an item of a recyclerview a name and choose a picture that should be displayed in this item.

Now my question: What is the best practice to do that. Make 2 empty arrays, one for the strings, the other one for the ressourceid's of the pictures, and store them there? Or is there a better way to do so.

Thanks in advance for your advices!

Daniel Bowen
Daniel Bowen
8,577 Points

Not 100% clear, are you wanting to keep the data paired or separately traversable?

4 Answers

Daniel Bowen
Daniel Bowen
8,577 Points

Especially with it being a string paired with a string, in other languages I would strongly recommend a tuple, and I found one a while back that works fairly well for me.

public class Tuple<X, Y> { 
  public final X x; 
  public final Y y; 
  public Tuple(X x, Y y) { 
    this.x = x; 
    this.y = y; 
  } 
} 
Seth Kroger
Seth Kroger
56,413 Points

FYI, Android has a built-in class for that: android.os.Pair

Daniel Bowen
Daniel Bowen
8,577 Points

Thanks, was actually unaware of that built-in class.

Lukas Baumgartner
Lukas Baumgartner
14,817 Points

In the fragments course where we built the smells like bakin app, ben used simple arrays to set the data for the single items for the list.

Can i use just arrays that have null valies and fill them with the data the user puts in?

Daniel Bowen
Daniel Bowen
8,577 Points

Well, as long as you know exactly how many values you'll have, that'd be fine. I assumed you were asking due to performance concerns, or simply learning.

Lukas Baumgartner
Lukas Baumgartner
14,817 Points

I finally made it with 2 arraylists, i use the add and remove method on both arraylists where I store the String in one list, and the ressource id in the other list. I'm trying to make use of a single array list where i store the string and the ressource id in. I'm sepperating them with a '. I keep you updated how it works out :)