简体   繁体   中英

How get ArrayList from RecyclerView.Adapter class

I'm beginner (and french), i have my MainActivity class and i use recyclerview with an adapter and it's work but now i want to call this arrayList (Notes) in my MainActivity

ArrayList<Note> Notes = new ArrayList<Note>();
mAdapter = new MyAdapter(Strings);
mRecyclerView.setAdapter(mAdapter);

from onBindViewHolder method from MyAdapter class :

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    holder.mButton.setText(Strings[position]);
    //it work

    Integer couleur = Notes.get(position).getCouleur();
    //i want to use Notes from my MainActivity for get a color
}

thanks for your answers :)

Add one more parameter to the constructor of RecyclerView inherited class and... Assign the value to arraylist class variable of class RecyclerView. Something like

....
....class MyRecyclerView extends RecyclerView....
{
List<xyz> arrayList;
....
public MyRecyclerView(...,ArrayList<xyz> arrayList)
{
this.arrayList=arrayList;
}

And use this.arrayList whereever you want to use.

Thanks a lot for your answer and i understand what does this code but i first i found this :

public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild {
public RecyclerView(Context context) {
    this(context, null);
}

and i can't modify the code. Second my arraylist use a bdd (this second code is in my MainActivity)

public NotesBDD noteBDD = new NotesBDD(this);
ArrayList<Note> Notess = noteBDD.ConstruireNotes();
    for (Note n : Notess){
        Notes.add(n);
    }

It's possible to use non-static methods from an other class ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM