Even thought you are in onClickListener anonymous class, you can
always get your context (instance of activity via
your_activity_class_name.this)

 cb.setOnClickListener(new OnClickListener()
        {
            TableLayout    tl;

            @Override
            public void onClick(View v)
            {
                if (((CheckBox) v).isChecked())
                {
                    if (tl == null)
                    {
                        Context c = MyActivity.this;


.............................

On Mar 27, 6:13 pm, Adrian Vintu <[email protected]> wrote:
> Np. There are many ways to get your desired behavior. I will post one of
> them, just in case someone needs it. This is actually a toggle layout, so
> it's a little bit more than what you asked.
>
>     cb.setOnClickListener(new OnClickListener()
>         {
>             TableLayout    tl;
>
>             @Override
>             public void onClick(View v)
>             {
>                 if (((CheckBox) v).isChecked())
>                 {
>                     if (tl == null)
>                     {
>                         Context c = v.getContext();
>
>                         TableRow tr = new TableRow(c);
>                         TextView b = new TextView(c);
>                         tl = new TableLayout(c);
>                         tl.removeView(tr);
>                         b.setText("Dynamic test");
>                         tr.addView(b);
>                         tl.addView(tr);
>
>                         LinearLayout parent = (LinearLayout) v.getParent();
>                         parent.addView(tl);
>                     }
>                 }
>                 else
>                 {
>                     LinearLayout parent = (LinearLayout) v.getParent();
>                     parent.removeView(tl);
>                     tl = null;
>                 }
>             }
>         });
>
> BR,
> Adrian Vintu
>
> http://adrianvintu.com
>
> On Sat, Mar 27, 2010 at 5:00 PM, rubeN_vl <[email protected]> wrote:
> > the v.getContext() did it!!!
> > ty dude
>
> > On 27 mrt, 16:48, Adrian Vintu <[email protected]> wrote:
> > > in the onClick you also have
>
> > > v.getContext();
> > > v.getParent();
> > > v.getRootView();
>
> > > On Sat, Mar 27, 2010 at 4:41 PM, Adrian Vintu <[email protected]>
> > wrote:
> > > > A. create one time the container view then use setVisibility() to show
> > or
> > > > hide it
> > > > or/and
> > > > B. you also have alert.getOwnerActivity() which gives you back the...
> > > > context :) you can then play around with it as you wish
>
> > > > On Sat, Mar 27, 2010 at 4:36 PM, rubeN_vl <[email protected]>
> > wrote:
>
> > > >> Same error,
>
> > > >> how can such a simple thing be so difficult :D
>
> > > >> if i create those outside the onclick scope, its like i can use them
> > > >> only one time.
> > > >> final TableRow tr = new TableRow(this);
> > > >> final TextView b = new TextView(this);
>
> > > >> On 27 mrt, 16:16, Adrian Vintu <[email protected]> wrote:
> > > >> > So toggle the visibility from GONE to VISIBLE. I think this would be
> > a
> > > >> quick
> > > >> > solution :)
>
> > > >> > BR,
> > > >> > Adrian Vintu
>
> > > >> >http://adrianvintu.com
>
> > > >> > On Sat, Mar 27, 2010 at 4:14 PM, Adrian Vintu <
> > [email protected]>
> > > >> wrote:
> > > >> > > It's my pleasure to help :)
>
> > > >> > > I think an easy solution is to use view.setVisibility(View.GONE)
>
> > > >> > > Does this work for you?
>
> > > >> > > BR,
> > > >> > > Adrian Vintu
>
> > > >> > >http://adrianvintu.com
>
> > > >> > > On Sat, Mar 27, 2010 at 4:10 PM, rubeN_vl <
> > [email protected]>
> > > >> wrote:
>
> > > >> > >>  checkboxrifle.setOnClickListener(new View.OnClickListener() {
> > > >> > >>            public void onClick(View v) {
> > > >> > >>                Patient p = (Patient) spinner.getSelectedItem();
> > > >> > >>                Service s = new Service("RIFLE");
> > > >> > >>                if (((CheckBox) v).isChecked()) {
> > > >> > >>                        p.addService(s,0);
> > > >> > >>                        DataAccess.registerService(p,s);
> > > >> > >>                        //TableRow tr = new TableRow(this);
> > > >> > >>                    //TextView b = new TextView(this);
> > > >> > >>                        //tr.removeView(b);
> > > >> > >>                        //TableLayout tl =
> > > >> > >> (TableLayout)findViewById(R.id.dynatabel);
> > > >> > >>                    //tl.removeView(tr);
>
> > > >> > >>                    // b.setText("Dynamic tekst");
> > > >> > >>                    // tr.addView(b);
> > > >> > >>                        //tl.addView(tr);
> > > >> > >>                } else {
> > > >> > >>                    p.removeService(0);
> > > >> > >>                    DataAccess.unregisterService(p, s);
> > > >> > >>                }
> > > >> > >>            }
> > > >> > >>        });
>
> > > >> > >> 1st of all tx for ur time.
> > > >> > >> So, i want to do this, push a checkbox, get an object in a
> > spinner,
> > > >> > >> add a row to a tabel. Uncheck the checkbox and the row gets
> > removed.
> > > >> > >> If i create textview outside the scope, i can add 1 new row to
> > the
> > > >> > >> table. Second time i check the checkbox i get the error described
> > > >> > >> previously.
> > > >> > >> The removeView() just removes the view, the application does not
> > > >> > >> crash, but no new rows are added. It just removes the previously
> > > >> > >> created View and creates a new one.
> > > >> > >> If i can create the textview inside the scope the prob would be
> > fixed
> > > >> > >> i think, just need something else then the 'this' when creatin a
> > new
> > > >> > >> one.
>
> > > >> > >> On 27 mrt, 15:58, Adrian Vintu <[email protected]> wrote:
> > > >> > >> > you should probably call removeView() :)) sorry, i could not
> > help
> > > >> it :))
>
> > > >> > >> > how about you just add the textview one time only?
>
> > > >> > >> > or how about you just post your code?
>
> > > >> > >> > BR,
> > > >> > >> > Adrian Vintu
>
> > > >> > >> >http://adrianvintu.com
>
> > > >> > >> > On Sat, Mar 27, 2010 at 3:52 PM, rubeN_vl <
> > [email protected]
>
> > > >> > >> wrote:
> > > >> > >> > > Yeah, figured out that yet, if i do that it works if i push
> > the
> > > >> button
> > > >> > >> > > 1 time, the second time i push it the application just
> > crashes.
> > > >> > >> > > Logcat gives me this error: You must call removeView() on the
> > > >> childs
> > > >> > >> > > parent first.
> > > >> > >> > > I need some way to 'flush' the textview.
>
> > > >> > >> > > On 27 mrt, 15:44, Adrian Vintu <[email protected]>
> > wrote:
> > > >> > >> > > > Your code probably looks like this
> > > >> > >> > > > AlertDialog a = builder.create();
>
> > > >> > >> > > > a.setButton(AlertDialog.BUTTON_POSITIVE, "Ok", new
> > > >> OnClickListener()
> > > >> > >> > > > {
> > > >> > >> > > >  public void onClick(DialogInterface dialog, int which)
> > > >> > >> > > >  {
> > > >> > >> > > >   TextView b = new TextView(this);
>
> > > >> > >> > > >    ...
>
> > > >> > >> > > > }
>
> > > >> > >> > > > You need to create the TextView outside the scope of the
> > > >> > >> OnClickListener.
> > > >> > >> > > > The "this" has to point to Context, which is your Activity.
>
> > > >> > >> > > > BR,
> > > >> > >> > > > Adrian Vintu
>
> > > >> > >> > > >http://adrianvintu.com
>
> > > >> > >> > > > On Sat, Mar 27, 2010 at 3:28 PM, rubeN_vl <
> > > >> [email protected]>
> > > >> > >> > > wrote:
> > > >> > >> > > > > Hi,
>
> > > >> > >> > > > > i'm trying to create a new TextView when a button is
> > clicked.
> > > >> > >> > > > > TextView b = new TextView(this); isnt working cause
> > 'this'
> > > >> refers
> > > >> > >> to
> > > >> > >> > > > > new View.onClickListener.
> > > >> > >> > > > > Been searchin for hours, cant find a solution.
>
> > > >> > >> > > > > --
> > > >> > >> > > > > You received this message because you are subscribed to
> > the
> > > >> Google
> > > >> > >> > > > > Groups "Android Developers" group.
> > > >> > >> > > > > To post to this group, send email to
> > > >> > >> > > [email protected]
> > > >> > >> > > > > To unsubscribe from this group, send email to
> > > >> > >> > > > > [email protected]<android-developers%[email protected]>
> > <android-developers%[email protected]<android-developers%[email protected]>
>
> > > >> <android-developers%[email protected]<android-developers%[email protected]>
> > <android-developers%[email protected]<android-developers%[email protected]>
>
> > > >> > >> <android-developers%[email protected]<android-developers%[email protected]>
> > <android-developers%[email protected]<android-developers%[email protected]>
>
> > > >> <android-developers%[email protected]<android-developers%[email protected]>
> > <android-developers%[email protected]<android-developers%[email protected]>
>
> > > >> > >> > > <android-developers%[email protected]<android-developers%[email protected]>
> > <android-developers%[email protected]<android-developers%[email protected]>
>
> > > >> <android-developers%[email protected]<android-developers%[email protected]>
> > <android-developers%[email protected]<android-developers%[email protected]>
>
> > > >> > >> <android-developers%[email protected]<android-developers%[email protected]>
> > <android-developers%[email protected]<android-developers%[email protected]>
>
> > > >> <android-developers%[email protected]<android-developers%[email protected]>
> > <android-developers%[email protected]<android-developers%[email protected]>
>
> > > >> > >> > > > > For more options, visit this group at
> > > >> > >> > > > >http://groups.google.com/group/android-developers?hl=en
>
> > > >> > >> > > > > To unsubscribe from this group, send email to
> > > >> android-developers+
> > > >> > >> > > > > unsubscribegooglegroups.com or reply to this email with
> > the
> > > >> words
> > > >> > >> > > "REMOVE
> > > >> > >> > > > > ME" as the subject.
>
> > > >> > >> > > --
> > > >> > >> > > You received this message because you are subscribed to the
> > > >> Google
> > > >> > >> > > Groups "Android Developers" group.
> > > >> > >> > > To post to this group, send email to
> > > >> > >> [email protected]
> > > >> > >> > > To unsubscribe from this
>
> ...
>
> read more »

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

To unsubscribe from this group, send email to 
android-developers+unsubscribegooglegroups.com or reply to this email with the 
words "REMOVE ME" as the subject.

Reply via email to