android - Dynamic Button Onclick Listener -


i trying create dynamic buttons. when clicking button should go specified url assigned text of button.

for testing, first tried id, if equal prints value of i. whenever clicked 1 button, instead of telling particular i value, enters whole loop, , prints values of i starting 1 19 (the number of buttons dynamically created)

and after printing values 1 19, program getting force closed saying null pointer exception.

i tried placing handler code outside oncreate(), i'm still getting same error.

for ( = 0; <itemlist.gettitle().size()-1; i++) {     title[i] = new textview(this);     title[i].settextcolor( -16711936 );     title[i].settextsize(18);     title[i].settext("title = "+itemlist.gettitle().get(i));     description[i] = new textview(this);     description[i].settextcolor(-16776961);     description[i].settext("description = "+itemlist.getdescription().get(i)+"......");     more[i]=new button(this);     more[i].settext(itemlist.getlink().get(i));      layout.addview(title[i]);     system.out.println("title view set");     layout.addview(description[i]);     //system.out.println("description view set");     layout.addview(more[i]);      more[i].setonclicklistener(listener); }  private onclicklistener listener=new onclicklistener(){     public void onclick(view arg) {         int index = 0;           (i = 0; < more.length; i++)           {               if (more[i].getid() == arg.getid())               {                   index = i;                   system.out.println("value of onclick is"+i);               }           }         //system.out.println("vlaue of in onclick"+i);         //uri uri=uri.parse(itemlist.getlink().get(i));         //startactivity(new intent(intent.action_view,uri));          //toast.maketext(getapplicationcontext(), "this button clicked"+i+more[i].gettext()+itemlist.getlink().get(i),toast.length_long).show();     } } 

you can use settag() , gettag() method of view identify different button.

for (i = 0; < itemlist.gettitle().size()-1; i++)  {     ...     more[i].settag(i);  // use index of itemlist tag } 

in onclick:

int index = (integer)arg.gettag(); 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -