java - Android Studio TextViews onClick all perform the same action, how do I fix it? -


i have function supposed create array of textviews unique ids.

each textview supposed perform unique action, when 1 of them clicked, perform function of last textview .

(ie, of them appends 9 last textview way set up) know why this, , how can fix it?

any appreciated.

//code:    @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_what_can_imake);          int textviewcount = 10;          textview[] textviewarray = new textview[textviewcount];         relativelayout mylayout = (relativelayout) findviewbyid(r.id.mylayout);         for(int = 0; < textviewcount; i++) {             textviewarray[i] = new textview(this);             textviewarray[i].settext("title"+integer.tostring(i));             textviewarray[i].setpadding(8,8+50*i,8,0);             textviewarray[i].setid(i);             layoutparams mytitledimensions = new relativelayout.layoutparams(                     layoutparams.wrap_content,layoutparams.wrap_content);               textviewarray[i].setonclicklistener(new view.onclicklistener()             {                 @override                 public void onclick(view v)                 {                     int myid = v.getid();                     ((textview) v).append(integer.tostring(myid));                 }             });             mylayout.addview(textviewarray[i],mytitledimensions);         }      } 

you using different paddingtop layout textviews vertically:

textviewarray[i].setpadding(8,8+50*i,8,0); 

this makes textviews visually separate each other, in fact overlapped, 2nd 1 overlapped 1st, 3rd 1 overlapped 2nd, etc. @ last, 9th 1 overlapped all, no matter text clicked, clicked 9th one.

to fix this, should change way layout textviews.

for example, use relativelayout.addrule(int verb, int anchor):

relativelayout.layoutparams mytitledimensions = new relativelayout.layoutparams( relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content); if (i > 0) {     mytitledimensions.addrule(relativelayout.below, - 1); } 

by way, 0 not valid id, 1st textview still overlapped 2nd one, change way generate ids little.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -