java - Give Id to every looped button and set on click listener based on button's id -


i have program retrieve data database, , show on screen buttons. how can give id every button created , make button.onclicklistener every button correct id?

here codes :

private void selectallgroup() { // todo auto-generated method stub mygroup = (textview) findviewbyid(r.id.tvlistgroup); database allgroup = new database(mygroupactivity.this); allgroup.open(); listgroup = allgroup.counthowmanygroups(username); layout = (linearlayout) findviewbyid(r.id.llmygroup);  string groupname[] = allgroup.fetchgroupname(username);  (int = 0; < listgroup; i++) {     newbt = new button(this);     newbt.settext(groupname[i]);     layout.addview(newbt); }  allgroup.close(); } 

and here database code (if needed) :

public int counthowmanygroups(string username) {     // todo auto-generated method stub     string query = "select " + group_name + " " + ms_group             + " inner join " + ms_group_detail + " b on a." + group_id             + "=b." + group_id + " " + member_username + "=?";      cursor c = ourdatabase.rawquery(query, new string[] { username });     int cnt = c.getcount();     c.close();     return cnt;  }  public string[] fetchgroupname(string username){     int i=0;      string query = "select " + group_name + " " + ms_group             + " inner join " + ms_group_detail + " b on a." + group_id             + "=b." + group_id + " " + member_username + "=?";     cursor c = ourdatabase.rawquery(query, new string[] { username });     string groupname[] = new string [c.getcount()];     int igroupname = c.getcolumnindex(group_name);      c.movetofirst();     (c.movetofirst(); !c.isafterlast(); c.movetonext()) {         groupname[i] = c.getstring(igroupname);         i++;     }      c.close();     return groupname; } 

regarding question "how can give id every button created , make button.onclicklistener every button correct id?"

simply use inside for-loop:

newbt.setid(id set) // should positive, doesn't have unique newbt.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 // todo auto-generated method stub                 //the view carries id                 v.getid();                 //then can set fast switch-case example                 switch (v.getid()) {                 case 1:                        break;                 default:                        break;             }         }); 

update

no problem cast button view in onclick-method , string:

newbt.setid(id set) // should positive, doesn't have unique newbt.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 // todo auto-generated method stub                 button myclickedbutton = (button) v;                 string buttontext = myclickedbutton.gettext();                  //do logic here.                  //you can switch-case on string ! afaik                  //it's possible on java compiler 1.7 or above.                  //but eclipse guide if want switch on string         }); 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -