2012-11-24 26 views
-1

我想在从另一个java类的服务器收到完整响应时完成活动。收到服务器的响应后完成活动

Category.java

这是主要活动

public void onListItemClick(ListView parent, View v, int position, long id) 
    { 
     // Get the selected category id & name. 
     String catId = ""; 
     String catName = ""; 
     LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1); 
     View view = ll.getChildAt(0); 
     if(view instanceof ListView) { 
      ListView lView = (ListView) view; 
      RowData rowData = (RowData) lView.getAdapter().getItem(position); 
      catId = rowData.mCatId; 
      catName = rowData.mTitle; 
     } 

     String url = "http://global.thinlenses.co.uk/virtualMirror/productlisting.php"; 
     String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "<category><Id>" + catId + "</Id>" 
        + "<Data>" + "pList" + "</Data></category>"; 

     if(getIntent().getBooleanExtra("VM", false)) 
      new SpinnerHelper().serverCall(Category.this, "ProductListVM", url, xml, catName); 
     else 
      new SpinnerHelper().serverCall(Category.this, "ProductList", url, xml, catName); 
    } 

SpinnerHelper.java随班就读不活动

void serverCall(Context context, final String target, final String url, final String xml, String extraValue) 
    { 
     try { 
      this.context = context; 
      this.target = target; 
      this.extraValue = extraValue; 

      ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
      if(conMan.getActiveNetworkInfo() != null && conMan.getActiveNetworkInfo().isConnected()) 
      { 
       dialog = new ProgressDialog(context); 
       dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
       dialog.setCancelable(true); 
       dialog.setTitle(R.string.app_name); 
       dialog.setMessage(context.getString(R.string.progressing)); 
       dialog.show(); 

       // Create a thread for updating the spinner. 
       Thread progressThread = new Thread(new Runnable() { 
        public void run() { 
         try { 
          while(!isExecuted) { 
           Thread.sleep(1000);         // Wait 1000ms between each update. 
           handler.sendMessage(handler.obtainMessage());  // Active the update handler. 
          } 
         } 
         catch (InterruptedException e) { 
          Log.e("VM", "Exception while spinner is updating at: " + e.getMessage()); 
         } 
        } 
       }); 
       progressThread.start(); 

       // Create a thread to get response from server. 
       Thread registerThread = new Thread(new Runnable() { 
        public void run() 
        { 
         // Get response XML from server and parse it. 
         String resXML = new Connection().getResponse(url, xml); 
         XMLParser parser = new XMLParser(); 
         Document doc = parser.getDomElement(resXML); 
         if(doc != null) 
         { 
          if(target.equals("Category")) { 
           NodeList nodeList = doc.getElementsByTagName("Category"); 
           name = new String[nodeList.getLength()]; 
           id = new String[nodeList.getLength()]; 

           // Fetch all node values and store into arrays. 
           for(int index = 0; index < nodeList.getLength(); index++) 
           { 
            Element element = (Element) nodeList.item(index); 
            id[index] = parser.getValue(element, "id"); 
            name[index] = parser.getValue(element,"CategoryName"); 
           } 
           result = "yes"; 
          } 
    } 

}

当我收到完整的效应初探该处理器被称为从服务器。

Handler handler = new Handler() 
    { 
    public void handleMessage(Message message) 
    { 
     if(isExecuted) 
     { 
      dialog.dismiss(); 
      isExecuted = false; 

      if(result.equals("yes")) 
      { 
        /////HERE i want to start the next activity and finish the Category(previous activity). 

      } 
+0

您的上下文引用你尝试做Category.this.finish()? – dougcunha

+0

是类别是活动,SpinnerHelper.java不是其常规类。 –

回答

1

你可以使用这个

Intent intent = new Intent(context,nextActivity.class); 
context.startActivity(intent); 
((Activity) context).finish(); 
相关问题