Last Button added Dictatorship in android -


i know title fancy going on in android app.

i storing names of websites user wants open in arraylist of strings. array of buttons made size depends on size of arraylist.

then set text of buttons using arraylist. works , buttons assigned correct text.

then set on click listeners of each button , create implicit intent open web site.

problem occuring despite fact text of buttons set , there problem listener because no matter button press, site of last button opened , hence last button dictatorship.

here code.

package com.example.hp.myproject;  import android.app.activity; import android.content.intent; import android.net.uri; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.linearlayout; import android.widget.textview;     import java.util.arraylist;  /**  * created @creativelabsworks   */     public class act3 extends activity      {      linearlayout ll1; button[] bt_arr; string s;      linearlayout.layoutparams params1;      arraylist<string> sites;textview t1;      protected void oncreate(bundle savedinstancestate)       {         super.oncreate(savedinstancestate);         intent intent=getintent();         sites= intent.getstringarraylistextra("arraylist");         /*creating linear layout hold edittexts*/         ll1=new linearlayout(this);         ll1.setorientation(linearlayout.vertical);         /*set width , height of linear layout*/         params1=new linearlayout.layoutparams(linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent);         setcontentview(ll1, params1);         /*initializing button array*/         bt_arr = new button[sites.size()];         /*initialize each button*/         for(int i=0;i<bt_arr.length;++i)         {             bt_arr[i] = new button(this);             ll1.addview(bt_arr[i]);         }         settext();         attach_listeners();     }     public void attach_listeners()     {         /*this function attaches listeners buttons in button array*/         for(int i=0;i<bt_arr.length;++i)         {             s=sites.get(i);             bt_arr[i].setonclicklistener(new view.onclicklistener()             {                 public void onclick(view v)                 {                     intent i=new intent(intent.action_view);                     i.setdata(uri.parse("http://"+s));                     startactivity(i);                 }             });         }     }     public void settext()     {         /*this function sets text of various buttons*/         int j;         for(int i=0;i<bt_arr.length;++i)         {             j=sites.get(i).indexof(".com");             bt_arr[i].settext(sites.get(i).substring(4,j));         }     }    } 

the problem have

 s=sites.get(i); 

s being set last element. whenever button clicked taking s last element since s outside onclicklistener instance have.

i suggest save url in button tag , use when button clicked.

for (int = 0; < bt_arr.length; ++i) {             s = sites.get(i);             bt_arr[i].settag(s);             bt_arr[i].setonclicklistener(new view.onclicklistener() {                 public void onclick(view v) {                     string website = (string)v.gettag();                     intent = new intent(intent.action_view);                     i.setdata(uri.parse("http://" + website));                     startactivity(i);                 }             });         } 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -