-1

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

类别.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 常规类不是 Activity

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).

             }
4

1 回答 1

1

您可以为此使用上下文参考

Intent intent = new Intent(context,nextActivity.class);
context.startActivity(intent);
((Activity) context).finish();
于 2012-11-24T16:56:10.177 回答