last day (16 days later) » 

11:09 PM
0
A: How to display an ArrayList in a RecyclerView?

Farbod Salamat-ZadehI remember when I was first reading about RecyclerViews - I agree it can be a little confusing at first. Hopefully, this explanation will help you understand it better. RecyclerView basics 1. Adding the RecyclerView First you need to add your RecyclerView to your XML layout. I'm assuming you...

 
Wow, thanks for this. I appreciate when people here don't stop at the short answer but go a little bit deeper in that. I've now a clearer vision of the RecyclerView now, for sure. Hoever I've just added one in the XML and then declared it in my Java code like this: RecyclerView myList = (RecyclerView) findViewById(R.id.charactersList); but Android Studio won't find the RecycleView class, how's that?
Solved this adding this line in build.gradle (Module: app) : 'com.android.support:recyclerview-v7:23.0.+'
Oh however I get stuck when implementing your Adapter class, at this line : Subject subject = player.getSubject(mContext); it can't find the getSubject() method, what should it be like?
I mean, I saw you added some methods, but all I need to display in each element is a String, nothing else, would that help to simplify the code?
 
@Snip3r_bow The one I gave with the Subject stuff was just one I had made for another app, and the subject part was a custom object I had made. You would change the relevant parts of the Adapter class for how you want use it.
 
Right. Got a runtime error, edited my question with it, any ideas?
 
@Snip3r_bow Your referenced layout in onCreateViewHolder should not be your activity but instead the layout for what a list item would look like. Perhaps that's where you made the mistake.
 
Mh, alright, any default layout in Android Studio to test that?
 
11:09 PM
@Snip3r_bow There's one called android.R.layout.simple_list_item_1 but the TextView won't have an ID of title. I think it's something like text1 but I'm on my phone and I can't remember from the top of my head.
 
Trying' right now
Is there a way I can look that up?
However, I've tried 'text' and it finds it, may that be?
 
@Snip3r_bow Probably. To check, hold Ctrl and click on where you have referenced android.R.layout.simple_list_item_1 - it should show you that layout file.
 
Good call man, it's text1 :)
But it doesn't find it here in my edited code: CoursesViewHolder(View itemView) { super(itemView); itemView.setOnClickListener(this); title = (TextView) itemView.findViewById(R.id.text1); }
Oh, I saw the layout had android. before the R, done the same for text1 and now it found it :) (Testing now)
 
@Snip3r_bow Glad I could help. Let me know if it works. :)
 
Hey! It works like a charm, but I have just 2 flash questions left
1 - How to display two texts in each element | 2 - I can get user clicked items right? It's not just for displaying? Or is it?
 
11:09 PM
@Snip3r_bow To display two texts, you need a list item layout that has two TextViews and then you use those in your constructor and onBindViewHolder method. If it's okay, I'll update my answer later (today or tomorrow) to show how to add the click listener. If you prefer, you can do some research to find out, but let me know so I can update my answer with information you haven't found.
 
Oh alright, thank you! I've made a layout myself to display those two texts in the same element, just like you said, two TextViews, now will edit it a bit up, but yeah surely need to get a click as a response as this is an activity started with the StartActivityForResult() method, and I need one of this elements as a result (the Player object). Thanks again and yeah see you soon!
If you're wondering, yeah I've another question opened about this StartActivityForResult(), you can check it out here
 
@Snip3r_bow No problem, glad I could help. I would appreciate it if you could accept and upvote my answer if it helped. I'll have a look at your question tomorrow, as I am revising for exams at school at the moment ;)
 
Unfortunately I can't upvote/downvote yet, but sure thing I accept this as the correct answer, wanted to do this since the first time I saw it ahah. Amazingly clear and well formatted answer, thank you for your help and have a great day! (And happy revising then :P )
 
@Snip3r_bow Thanks ;) I'll update my answer soon with your question and let you know. I'll also have a look at your other question tomorrow.
 
Hey Farbod, tried to google about getting response from an item click of a RecyclerView, but not sure how to implement that in my code, look, I've found this tutorial, any ideas?
 
11:09 PM
@Snip3r_bow Thanks for the reminder. I've updated my answer to some points you made in the comments, and another section on how to add an OnClickListener for your RecyclerView. There were lots of examples already available on Stack Overflow, but I have provided an explanation as well in case you were a bit unsure.
 
You're amazing. Thank you so much for this, tomorrow when I'll get up reading your reply will be the first thing I'll do (12:34 AM)
Hey, now I have the position how do I pass it to the RecyclerView so I can get that item at that position? Like myList.getItem(position);
 
@Snip3r_bow I'm not sure what you mean. You can access the position of the item in the RecyclerView when it has been clicked (via the onEntryClick method). You don't need to pass it back to the RecyclerView or anything like that. If you want to get the CustomClass object from the item that you clicked you can use yourCustomArrayList.get(position) in onEntryClick.
 
Yeap, I used that on the ReyclerView, not a great move ahah Now I just need to get a response back, may I ask you to get a look at my other question if you can? Still need to find an answer to that
Hey, one last question here, as it's more about the RecyclerView than the other question of mine: how do I edit the text displayed in the RecyclerView? I basically need to store somewhere each time a certain element of the list's been clicked, so I thought (very roughly) to add such number inside parenthesis next to the player's name, which is in the RecyclerView. So how may I edit that holder.name.setText(player.getName()); I set previously in the Adapter Class?
 
@Snip3r_bow You would need to update your ArrayList<Player> with the changes (you'd probably need to have this extra number as part of your player object). Then update the RecyclerView - there is a particular method for this but I can't remember the name of it so you might have to look it up.
 
As this discussion is taking way too long, you mind going for 5 minutes in chat? Won't be long there neither! It's just to keep here 'clean' (Unfortunately for 1 rep I can't do this myself)
Because I need to edit the text displayed there, but I can't modify directly what's in the ArrayList, infact I've set the text (of the RecyclerView) accessing some properties of the Players objects inside the Arraylist. How do I edit the displayed text? Like I've firstly did in the Adapter?
 

  last day (16 days later) »