android - How to access all child elements of a GridView? -


i have gridview showing items of "icon text overlay". on specific event want iterate of items , change text. how can reference items outside of onitemclicklistener?

can somehow execute like: view.findallviewbyid(r.id.itemtext);? know findall() not exist.

item_view.xml:

<relativelayout>     <imageview />     <textview android:id="@+id/itemtext" /> </relativelayout> 

main.xml:

<linearlayout>     <gridview android:id="@+id/simplegridview" /> </linearlayout> 

code.

gridview grid = (gridview) findviewbyid(r.id.simplegridview); grid.setadapter(..); //inside adapter: inflates items r.layout.item_view  grid.setonitemclicklistener(new adapterview.onitemclicklistener() {     @override     public void onitemclick(adapterview<?> parent, view view, int position, long id) {         textview item = ((textview) view.findviewbyid(r.id.itemtext));         item.settext("some click text");     } }); 

question: how can access available r.id.itemtext elements, , iterate through them? because have no reference them.

1. child views

if want access r.id.itemtext views use gridview viewgroup (it extends it), iterate on children , findviewbyid(r.id.itemtext) each of them:

for (int = 0; < grid.getchildcount(); i++) {     view child = getchildat(i);     view itemtext = child.findviewbyid(r.id.itemtext);     ... } 

2. adapter items

but gridview adapterview , has handy methods getfirstvisibleposition, getlastvisibleposition , getitematposition , consider using them data in (currently visible) text views this:

int first = grid.getfirstvisibleposition(); int last = grid.getlastvisibleposition(); for(int i=first; i<=last; ++i){     object item = grid.getitematposition(); // should cast adapter's item type } 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -