java - Android - ArrayList and RecycleView.Adapter -
i have got chordslistactivity
looks this:
public class chordslistactivity extends activity { private recyclerview chordslist; private recyclerview.layoutmanager listlayoutmanager; private arraylist<accordo> getchords() { arraylist<accordo> chords = new arraylist<>(); accordo a=new accordo(); a.setname("do maggiore"); a.setnote("do, mi, sol"); a.setimage(r.drawable.do_maggiore); chords.add(a); a=new accordo(); a.setname("do 5"); a.setnote("na, na, na"); a.setimage(r.drawable.do5); chords.add(a); return chords; } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_chords_list); chordslist = (recyclerview) findviewbyid(r.id.chords_recycler); //use linear layout manager listlayoutmanager = new linearlayoutmanager(this); chordslist.setlayoutmanager(listlayoutmanager); chordslistadapter adapter = new chordslistadapter(this, getchords()); /** start searchactivity when imagebutton pressed */ imagebutton cerca = (imagebutton) findviewbyid(r.id.search); cerca.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(getapplicationcontext(), searchactivity.class); arraylist<accordo> chords = new arraylist<accordo>(); intent.putextra("chords", chords); startactivity(intent); } }); } }
there error on line:
chordslistadapter adapter = new chordslistadapter(this, getchords());
this error:
error:(126, 38) error: constructor chordslistadapter in class chordslistadapter cannot applied given types; required: arraylist found: chordslistactivity,arraylist reason: actual , formal argument lists differ in length
and chordslistadapter
:
public class chordslistadapter extends recyclerview.adapter<chordslistadapter.myviewholder> { private arraylist<accordo> chordslist; public class myviewholder extends recyclerview.viewholder { public textview nome, note; public imageview immagine; public myviewholder(view view) { super(view); nome = (textview) view.findviewbyid(r.id.nome); note = (textview) view.findviewbyid(r.id.note); immagine = (imageview) view.findviewbyid(r.id.immagine_accordo); } } public chordslistadapter(arraylist<accordo> chordslist) { this.chordslist = chordslist; } @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { view itemview = layoutinflater.from(parent.getcontext()).inflate(r.layout.row, parent, false); return new myviewholder(itemview); } @override public void onbindviewholder(myviewholder holder, int position){ accordo chord = chordslist.get(position); holder.nome.settext(chord.getnome()); holder.note.settext(chord.getnote()); holder.immagine.setimageresource(chord.getimage()); } @override public int getitemcount() { return chordslist.size(); } }
could me figure out problem , how fix it?
this line
chordslistadapter adapter = new chordslistadapter(this, getchords());
is call constructor of chordslistadapter
, looks in chordslistadapter.class
:
public chordslistadapter(arraylist<accordo> chordslist) { this.chordslist = chordslist; }
either you'll need change constructor accept context
or activity
.
public chordslistadapter(context context, arraylist<accordo> chordslist) { this.chordslist = chordslist; }
or can delete first parameter in call:
chordslistadapter adapter = new chordslistadapter(getchords());
Comments
Post a Comment