android - getAdapterPosition() not returning position of item in RecyclerView -


this sort of follow-up or complement this post. trying position of item in recyclerview, none of methods have tried have worked.

i called getadapterposition() in personviewholder constructor , assigned value integer position, used in unitonefragment class accordingly (see onclick() method). whatever is, doesn't happen , guess because getadapterposition() method doesn't work or isn't used correctly.

my adapter:

public class rvadapter extends recyclerview.adapter<rvadapter.personviewholder> {  context mcontext;  rvadapter(context context, list<chapter> chapters) {     mcontext = context;     this.chapters = chapters; }  public static cardview cv; public static int position; public static string chapternumbertitle;  public class personviewholder extends recyclerview.viewholder implements view.onclicklistener {     textview chaptername;     textview chapternumber;     // imageview chapterphoto;      public personviewholder(view itemview) {         super(itemview);          cv = (cardview) itemview.findviewbyid(r.id.cv);         chaptername = (textview) itemview.findviewbyid(r.id.chapter_name);         chapternumber = (textview) itemview.findviewbyid(r.id.chapter_number);         // chapterphoto = (imageview) itemview.findviewbyid(r.id.person_photo);          chapternumbertitle = chapternumber.gettext().tostring();         position = getadapterposition();          itemview.setonclicklistener(this);     }      @override     public void onclick(view v) {         unitonefragment.chapterchecker();          intent intent = new intent(mcontext, chapteractivity.class);         mcontext.startactivity(intent);     } }  list<chapter> chapters;  @override public void onattachedtorecyclerview(recyclerview recyclerview) {     super.onattachedtorecyclerview(recyclerview); }  @override public personviewholder oncreateviewholder(viewgroup viewgroup, int i) {     view v = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.item, viewgroup, false);     personviewholder pvh = new personviewholder(v);     return pvh; }  @override public void onbindviewholder(personviewholder personviewholder, int i) {     personviewholder.chaptername.settext(chapters.get(i).chaptername);     personviewholder.chapternumber.settext(chapters.get(i).chapternumber);     // personviewholder.chapterphoto.setimageresource(persons.get(i).photoid);  }  @override public int getitemcount() {     return chapters.size();     } } 

the other class:

public class unitonefragment extends android.support.v4.app.fragment {  private list<chapter> chapters; private recyclerview rv; private recyclerview.layoutmanager llm;  public unitonefragment() {     // required empty public constructor }   @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     // inflate layout fragment     view v = inflater.inflate(r.layout.fragment_unit_one, container, false);      getactivity().settitle("unit one");      rv = (recyclerview) v.findviewbyid(r.id.rv);     rv.sethasfixedsize(true);      llm = new linearlayoutmanager(getactivity());     rv.setlayoutmanager(llm);      initializedata();     initializeadapter();      return v; }  private void initializedata() {     chapters = new arraylist<>();     chapters.add(new chapter("human prehistory civilizations", "chapter one"));     chapters.add(new chapter("classical china", "chapter two"));     chapters.add(new chapter("classical india", "chapter three"));     chapters.add(new chapter("classical greece , rome", "chapter four"));     chapters.add(new chapter("classical period: declines , diversities", "chapter five")); }  private void initializeadapter() {     rvadapter adapter = new rvadapter(getactivity(), chapters);     rv.setadapter(adapter); }  public static void chapterchecker() {      if (rvadapter.position == 0) {         summaryfragment.summarytext.settext("contrary popular belief, lorem ipsum not random text. has roots in piece of classical latin literature 45 bc, making on 2000 years old. richard mcclintock, latin professor @ hampden-sydney college in virginia, looked 1 of more obscure latin words, consectetur, lorem ipsum passage, , going through cites of word in classical literature, discovered undoubtable source. lorem ipsum comes sections 1.10.32 , 1.10.33 of \"de finibus bonorum et malorum\" (the extremes of , evil) cicero, written in 45 bc. book treatise on theory of ethics, popular during renaissance. first line of lorem ipsum, \"lorem ipsum dolor sit amet..\", comes line in section 1.10.32.");         }     } }  class chapter { string chaptername; string chapternumber; // int photoid;  chapter(string chaptername, string chapternumber) {     this.chaptername = chaptername;     this.chapternumber = chapternumber;     // this.photoid = photoid;     } } 

my code seems pretty self-explanatory if there's confusing, please let me know , add more information. once again, problem can't position of item in recyclerview , act on it.

call getadapterposition @ onclick method because @ time of click u want know position of item. if u check @ time of creation (most of times) inaccurate.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -