0

I have a for loop which creates views dynamically.

  for (int i=0;i<5;i++){ 
a=new ImageView();
tv=new TextView();
img=new ImageView();
spn=new Spinner();
img.setOnClickListener(this);//this is working for every view

spn.post(new Runnable){
@Override
public void run() {
Log.d("post","inside post");// here only last view is working
}


} //for loop ends

But how to create spinner.post dynamically or make it work. Please help. Stuck on it for last three days.Thanks in advance.

Anand.B
  • 141
  • 1
  • 2
  • 11
  • and what does it have to do with threads? – pskink Jul 31 '14 at 06:21
  • I am trying to do this http://stackoverflow.com/a/9375624/3796274 – Anand.B Jul 31 '14 at 06:32
  • i still don't know what you're stuck on for three days... – pskink Jul 31 '14 at 07:13
  • I am setting this in a tablelayout dynamically.One by one.So there are five spinners in tablelayouts. when i click any one it should post log.But i am getting log only for the fifth one(Spinner). – Anand.B Jul 31 '14 at 07:37
  • did you try setOnItemSelectedListener? – pskink Jul 31 '14 at 08:01
  • yes. it worked with setOnItemClickListener. But the problem is I want to place setOnItemClickListener inside run() method. – Anand.B Jul 31 '14 at 08:49
  • inside run() method? what do you mean? why do you want so? – pskink Jul 31 '14 at 09:09
  • I am calling setonclicklistener inside asynctask's onpostexecute method.but the problem is setonitemselected method is called on initialisation and this method(setonitemselected) bydefault gets executed everytym the async is called without (user interacting with the spinner). To avoid this i am using spinner.post().But the above problem arises. – Anand.B Jul 31 '14 at 09:23

1 Answers1

0

Make your activity implementes OnItemSelectedListener, which is the callbak registered for every item selected in the spinner, and then inside this callback implements what you need on every view.

Your class definition:

  public class SpinnerActivity extends Activity implements OnItemSelectedListener

Implements onItemSelected callback in your activity:

  public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    // parent.getItemAtPosition(pos)
  }

If you need documentation look at this http://developer.android.com/guide/topics/ui/controls/spinner.html ;)

axl coder
  • 739
  • 3
  • 19