If you user the Looper and another handler to your class F, it would
work.


Code marked with //NEW

class F extends Thread{

    Context context;
    TextView statusField;
    Handler handler;
    String voto="";

    F (Context context,TextView statusField,Handler handler)    {
        this.context=context;
        this.statusField=statusField;
        this.handler=handler;
    }
    public Handler mHandler;

    public void run()
    {
        Looper.prepare(); //NEW

        final CharSequence[] items = {"Ottimo", "Buono",
                "Sufficiente","Insufficiente"};

             handler.post(new Runnable(){

                        public void run(){
                            AlertDialog.Builder builder = new
AlertDialog.Builder(context);

                            builder.setTitle("Pick a color");
                            builder.setSingleChoiceItems(items, -1,
new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface
dialog, int item) {
                                    Toast.makeText
(context.getApplicationContext(), items [item],
Toast.LENGTH_SHORT).show();
 
dialog.cancel();
                                    statusField.setText(items[item]);
                                    Message m = new Message(); //NEW
                                    m.what = 1; //NEW, any id is fine,
a const would be better :)
                                    m.arg1 = item; //NEW
                                    mHandler.sendMessage(m); //NEW

                                }
                            });
                            AlertDialog alert = builder.create();
                            alert.requestWindowFeature
(Window.FEATURE_NO_TITLE);
                            alert.getWindow().setFlags
(WindowManager.LayoutParams.FLAG_FULLSCREEN,
 
WindowManager.LayoutParams.FLAG_FULLSCREEN);
                            alert.show();

                    }});

                    mHandler = new Handler() { //NEW
                        public void handleMessage(Message msg) {
                            switch (msg.what) {
                            case 1:
                                voto = items[msg.arg1].toString();
                                Log.d("Class F" , voto);
                                break;
                            default:
                                super.handleMessage(msg);
                        }
                        }
                    };


                    Looper.loop(); // NEW
}

You can read more at 
http://developer.android.com/reference/android/os/Looper.html

Balwinder Kaur
Open Source Development Center
·T· · ·Mobile· stick together

The views, opinions and statements in this email are those of the
author solely in their individual capacity, and do not necessarily
represent those of T-Mobile USA, Inc.


On Aug 4, 4:15 pm, Lorenz <[email protected]> wrote:
> Thanks Kaur,it works..
> but if I want to do some action in the thread after the user has
> choose ???Namely there is a way to do something like this..:(I've used
> NEW )
>
> public void run()
>
>
>
> >     {
>
> >                     final CharSequence[] items = {"Ottimo", "Buono",
> > "Sufficiente","Insufficiente"};
>
> >                     String voto="";
>
> >                    handler.post(new Runnable(){public void run(){
> >                             AlertDialog.Builder builder = new
> > AlertDialog.Builder(context);
>
> >                             builder.setTitle("Pick a color");
> >                             builder.setSingleChoiceItems(items, -1,
> > new
> > DialogInterface.OnClickListener() {
> >                                 public void onClick(DialogInterface
> > dialog, int item) {
> >                                     Toast.makeText
> > (context.getApplicationContext(), items
> > [item], Toast.LENGTH_SHORT).show();
> >                                     //======== START =======
> >                                     dialog.cancel();
> >                                     statusField.setText(items[item]);
> >                                     //========= END =========
>
>                                  voto=items[item];  <----------NEW
>
> >                                 }
> >                             });
> >                             AlertDialog alert = builder.create();
> >                             alert.show();
>
> >                     }});
>
> > }
> > }
> >      if(voto=="ottimo") do something..  <--------NEW
>
> It seems that the code doesn't stop, so the string voto could be
> empty...
> I need to use in the thread the choiche of the the user..
> If you have some idea..
> Thanks
>
> On 4 Ago, 17:33, "Balwinder Kaur (T-Mobile)" <balwinder.k...@t-
>
> mobile.com> wrote:
> > If you change your code in a few places, it should work. (It worked
> > for me :))
>
> > I have demarked the code changes with
> > //======== START====  and //======= END ====
>
> > public void onCreate(Bundle savedInstanceState) {
> >     super.onCreate(savedInstanceState);
> >     setContentView(R.layout.main);
> >    Handlerhandler=newHandler();
> >     //====== START ====
> >     // 1. I assume you have a TextView defined in  your layout.main
> > file.
> >     // 2. Make sure you have android:id set e.g. <TextView
> > android:id="@+id/tview"
> >     TextView statusField = (TextView) findViewById(R.id.tview);
> >     //====== END ======
> >     F thread=new F(this,statusField,handler);
> >     thread.start();
>
> > }
> > }
>
> > class F extends Thread{
>
> >     Context context;
> >     TextView statusField;
> >    Handlerhandler;
> >     String voto;
>
> >     F(Context context,TextView statusField,Handlerhandler)
> > {this.context=context;this.statusField=statusField;this.handler=handler;}
> >     public void run()
> >     {
>
> >                     final CharSequence[] items = {"Ottimo", "Buono",
> > "Sufficiente","Insufficiente"};
>
> >                     String voto="";
>
> >                    handler.post(new Runnable(){public void run(){
> >                             AlertDialog.Builder builder = new
> > AlertDialog.Builder(context);
>
> >                             builder.setTitle("Pick a color");
> >                             builder.setSingleChoiceItems(items, -1,
> > new
> > DialogInterface.OnClickListener() {
> >                                 public void onClick(DialogInterface
> > dialog, int item) {
> >                                     Toast.makeText
> > (context.getApplicationContext(), items
> > [item], Toast.LENGTH_SHORT).show();
> >                                     //======== START =======
> >                                     dialog.cancel();
> >                                     statusField.setText(items[item]);
> >                                     //========= END =========
> >                                 }
> >                             });
> >                             AlertDialog alert = builder.create();
> >                             alert.show();
>
> >                     }});
>
> >                     //statusField.setText(voto); //REMOVE
>
> > }
> > }
>
> > Hope this helps,
> > Balwinder Kaur
> > Open Source Development Center
> > ·T· · ·Mobile· stick together
>
> > The views, opinions and statements in this email are those of the
> > author solely in their individual capacity, and do not necessarily
> > represent those of T-Mobile USA, Inc.
>
> > On Aug 4, 5:48 am, Lorenz <[email protected]> wrote:
>
> > > Hi,
> > > I have a problem, I want to create an Alert Builder in a thread
> > > started by tha main activity.This Builder should be a multiple
> > > choiche.
> > > The code is:
>
> > > public class C extends Activity {
>
> > >         private PrintWriter savedpoint;
> > >         private Context context = this;
> > >         private TextView statusField;
>
> > >     @Override
> > >     public void onCreate(Bundle savedInstanceState) {
> > >         super.onCreate(savedInstanceState);
> > >         setContentView(R.layout.main);
> > >        Handlerhandler=newHandler();
>
> > >         F thread=new F(this,statusField,handler);
> > >         thread.start();
>
> > >     }
>
> > > }
>
> > > class F extends Thread{
>
> > >         Context context;
> > >         TextView statusField;
> > >        Handlerhandler;
> > >         String voto;
>
> > >         F(Context context,TextView statusField,Handlerhandler)
> > > {this.context=context;this.statusField=statusField;this.handler=handler;}
> > >         public void run()
> > >         {
>
> > >                         final CharSequence[] items = {"Ottimo", "Buono",
> > > "Sufficiente","Insufficiente"};
>
> > >                         String voto="";
>
> > >                        handler.post(new Runnable(){public void run(){
> > >                                 AlertDialog.Builder builder = new 
> > > AlertDialog.Builder(context);
> > >                                 builder.setTitle("Pick a color");
> > >                                 builder.setSingleChoiceItems(items, -1, 
> > > new
> > > DialogInterface.OnClickListener() {
> > >                                     public void onClick(DialogInterface 
> > > dialog, int item) {
> > >                                         
> > > Toast.makeText(context.getApplicationContext(), items
> > > [item], Toast.LENGTH_SHORT).show();
>
> > >                                     }
> > >                                 });
> > >                                 AlertDialog alert = builder.create();
> > >                                 alert.show();
>
> > >                         }});
>
> > >                         statusField.setText(voto);
>
> > >    }
>
> > > }
>
> > > Problem 1: what can I do for doing some actions when a user select one
> > > of the alert choiches?
> > > Problem 2: It seems that the code doesn't stop  when the alert dialog
> > > appears but still run, as a matter of fact the statusField is empty
> > > when appears.
> > > If nothing is possible to do Is there any other way to obtain
> > > something like this working?
> > > THanks
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" 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-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to