android - Select only one table row and Deselect others -
i have table layout table rows being added dynamically database. need programmatically choose 1 table row , deselect previous choosen. similar listview onitemclick. reasons can't use listview.
you add click listener on table rows. see code below:
private int selectedindex = -1; private tablelayout tablelayout; private void inittablelayout() { view.onclicklistener clicklistener = new view.onclicklistener() { @override public void onclick(view v) { integer index = (integer) v.gettag(); if (index != null) { selectrow(index); } } }; tablelayout = (tablelayout) findviewbyid(r.id.table_layout); final tablerow tr = new tablerow(this); tr.settag(0); tr.setonclicklistener(clicklistener); // add views on tr tablelayout.addview(tr, 0, new tablelayout.layoutparams( tablerow.layoutparams.match_parent, tablerow.layoutparams.wrap_content)); final tablerow tr2 = new tablerow(this); tr2.settag(1); tr2.setonclicklistener(clicklistener); // add views on tr2 tablelayout.addview(tr2, 1, new tablelayout.layoutparams( tablerow.layoutparams.match_parent, tablerow.layoutparams.wrap_content)); .... } private void selectrow(int index) { if (index != selectedindex) { if (selectedindex >= 0) { deselectrow(selectedindex); } tablerow tr = (tablerow) tablelayout.getchildat(index); tr.setbackgroundcolor(color.gray); // selected item bg color selectedindex = index; } } private void deselectrow(int index) { if (index >= 0) { tablerow tr = (tablerow) tablelayout.getchildat(index); tr.setbackgroundcolor(color.transparent); } }
Comments
Post a Comment