• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps using handles to update widgets

manish411

Lurker
I am making a server-client application in which a client can download files from a server. I am building the client side as an android application.

I am creating a Handler class as follows which is suppose to call the list()
function which displays a dialog box with list of the result returned by the server;
Code:
    Handler handler = new Handler(){
            public void handleMessage(Message msg)
            {
                String result [] = (String[])msg.obj;
                System.out.println("Calling list function");
                 list(result);
        }
        };

public void list(String a[])
{
    final String items[] =a;
    AlertDialog.Builder builder = new AlertDialog.Builder(Search.this);

    //Set it title

    builder.setTitle("Download");

    //Set the list items and assign with the click listener

    builder.setItems(items, new DialogInterface.OnClickListener() {

    // Click listener

    public void onClick(DialogInterface dialog, int item) {
          Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();

    }   });
    AlertDialog alert = builder.create();

    //display dialog box

    alert.show();

   //sk.setTexat(d.readUTF());

        }
initialize() is used to initialize and the class also defines an onClick() action which runs the background thread which will send a message (result obtained from the server to display as a dialog list)
Code:
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.opening);
        try {
            initialize();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
   
    }

public void initialize() throws IOException
{
    music = (RadioButton)findViewById(R.id.music);
    videos = (RadioButton)findViewById(R.id.video);
    books = (RadioButton)findViewById(R.id.books);
    d = (EditText)findViewById(R.id.Search);
    k = (TextView)findViewById(R.id.connection);
    b = (Button)findViewById(R.id.Go);
    sock = staticsocket.socket;
    System.out.println(sock.getRemoteSocketAddress());
    k.setText(sock.getRemoteSocketAddress().toString());
    b.setOnClickListener(this);
    dout = new DataOutputStream(sock.getOutputStream());
    
    filetype = "Music";
    
}
    
@Override
public void onClick(View v) {
    
    filename = d.getText().toString();
//String  name[] = {"hello","world"};
  String name[] = new String[10];
    try {
    dout.writeUTF(filetype);
    dout.writeUTF(filename);
    }
    catch(Exception e)
    {e.printStackTrace();}
   
    Thread o= new Thread(new Runnable()
    {
     String filepath = null;    
        public void run()
        {
            String resulta[];
             resulta = new String[10];
            try{
             DataInputStream din = new DataInputStream(staticsocket.socket.getInputStream());
            //  String resulta[]= new String[10];
              String result;
              int i = 0;
                      while((result=din.readUTF()).equals("finished")==false)
              {
                  resulta[i] = result;
                 
             
              }
                    
            }
       catch(Exception e)
       {
           e.printStackTrace();
       }
        
        Message msg = handler.obtainMessage();
        msg.obj = resulta;
        handler.sendMessage(msg);
        }
    }); 
  o.start();

  System.out.println("thread started");
  
}


}
The problem is that I cannot call the function list() from within the handleMessage() method defined above . The application collapses . How do I update the screen whose action depends on a background thread?
Also I cannot call another activity from within the handleMessage() method.
 
Back
Top Bottom