java - Why getItem(position) works with a new instance? (should not be zero?) -


i'm trying learn java, read casting, instantiate objects, arrays, hashmaps, etc. making codes android , doing until found myself this:

myobject instobject = (myobject)getitem(position);   

from i've learned far... i'm creating new instance of myobject in instobject, so, think value should zero. then, when call settext(...), works! =o

please try not summarize answer because understand has been difficult me far!
speak spanish, know little english not well.

public class myclass extends listfragment {     list<myobject> instobject;  //instobject = 0     ...      @override     public void onactivitycreated(bundle savedinstancestate){         super.onactivitycreated(savedinstancestate);          instobject = new arraylist<myobject>();         myobject item = ...         instobject.add(item); //instobject = items     }      //adapter{         ...          @override         public view getview(int position, view view, viewgroup parent) {             myobject instobject = (myobject)getitem(position);             //instobject = ? (should not zero?)             holder.title.settext( instobject.gettitle() );             // why return corresponding title?         }       }      public class myobject {           vars...           constructor...           setters...           getters...       } } 

solved thanks!!! :)

i had 3 confusions...

first:
thought if "instobject", created inside of "onactivitycreated", "instobject", no accessed inside adapter. (and can't) inside of adapter, "instobject", zero, but... forgot inside of constructor's adapter, "instobject", again items created in "onactivitycreated". (sorry)

second:
thought might item this:

item = instobject.getitem(position);  //well i'm newbie!  :p 

and then, content this:

myview.settext(item.gettitle()); 

third:
overlooked method getitem(), had this:

@override public object getitem(int position) {     return instobject.get(position); }   

before getview(), "instobject", had items, with:

myobject instobject = (myobject)getitem(position); 

in technical language, think maybe this:
make reference of "instobject", in specific position.

without technical...
create variable of type object, same name, , set 1 of items of "instobject".

i think got it, again! :)

it depends on getitem() method inside adapter does. speaking when getitem(), adapter returns reference object @ particular position collection passed adapter while creating adapter.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -