android - Difference between view.findViewById(R.id....) and just findViewById(R.id...) -


i'm trying implement custom array adapter ,

problem code crashes when use

 imageview imageview=(imageview)findviewbyid(r.id.imageview); 

rather

imageview imageview=(imageview)rowview.findviewbyid(r.id.imageview); 

where rowview layout file listview have infalted.

why occur , thought rowview.findviewbyid(r.id..) make faster find element id, without app crashes, can please explain

this activity code

public class mainactivity extends appcompatactivity {

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      //now list view      listview listview=(listview)findviewbyid(r.id.listview);      string[] values =new string[]{"ios","android","firefoxos","ubuntu"};     mysimpleadapter simpleadapter=new mysimpleadapter(this,values);     listview.setadapter(simpleadapter);     //      }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.menu_main, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); }  class mysimpleadapter extends arrayadapter<string>{      private context context;     private string[] values;      @override     public view getview(int position, view convertview, viewgroup parent) {     //listview calls on adapter each row         log.v("list","getview");         layoutinflater inflater = (layoutinflater) context                 .getsystemservice(context.layout_inflater_service);          view rowview=inflater.inflate(r.layout.custom_row,parent,false);          textview textview1= (textview) rowview.findviewbyid(r.id.textview1);          textview1.settext(values[position]);         //for android row alone set image , others leave blank         if(values[position].startswith("and"))         {             imageview imageview=(imageview)rowview.findviewbyid(r.id.imageview);             imageview.setimageresource(r.drawable.ic_launcher);//ok r.drawale....is id         }           return rowview;     }      public mysimpleadapter(context context,string[] values) {         super(context, r.layout.custom_row,values);          this.context=context;         this.values=values;         log.v("list","constructor");     } } 

}

findviewbyid aways requires context find view.

if calling class extended activity, use findviewbyid, because activity context.

when using in class extends fragment, have use getactivity().findviewbyid

in case, since using in adapter, need find thet view within listview row. use view.findviewbyid


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -