如何在AlertDialog中调用外部方法?

时间:2012-09-07 04:21:00

标签: android

我有一个方法用于连接AppTools.java中的HttpGet:

public class AppTools extends Activity {
public void connectToServerWithURL(String URL)
        throws ClientProtocolException, IOException, JSONException {
/* Start connect */
    new Thread() {
client = new DefaultHttpClient();
request = new HttpGet(URL);
response = client.execute(request);
reader = new BufferedReader(new InputStreamReader(response.getEntity()
            .getContent()));
builder = new StringBuilder();
    for (String s = reader.readLine(); s != null; s = reader.readLine()) {
        builder.append(s);
    }

if (builder != null) {
        /* Transfer to JSONArray */
        jsonTransfer = new JSONObject(builder.toString());
        systemConfigJSONArray = jsonTransfer.getJSONArray(config);
        runOnUiThread(performResult);
    }
       }.start();
} 
private Runnable performResult = new Runnable() {
    public void run() {
        closeProgressDialog();
        performResult(systemConfigJSONArray);
    }
};

/** Connect complete, interface for Override **/
public void performResult(JSONArray resultArray) {
}

而且,在另一个Activity中扩展了AppTools:

public class A extends AppTools { 
活动A中的

有一个AlertDialog用于选择要连接的URL:

new AlertDialog.Builder(this).setView(aLayout)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int        which) {
                    /* Set command */
                    if (spinner_options.getSelectedItemPosition() == 0) {
                        connectToServerWithURL("http://...");
                    } else if (spinner_options
                            .getSelectedItemPosition() == 1) {
                        connectToServerWithURL("http://...");
                    }
                }
            }).setNegativeButton("Cancel", null).show();

.setView(aLayout)中的aLayout有一个Spinner,所以setPositiveButton的onClick接口

将获得微调器的选定位置并执行方法

但代码无法正常工作,LogCat显示AlertController $ ButtonHandler错误

有什么问题?

1 个答案:

答案 0 :(得分:2)

//Define String before OnCreate() method

String url;


new AlertDialog.Builder(this).setView(aLayout)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int        which) {
                    /* Set command */
                    if (spinner_options.getSelectedItemPosition() == 0) {
                        url="http://...";
                         new YourAsyncTask().execute();
                      //  connectToServerWithURL("http://...");

                    } else if (spinner_options
                            .getSelectedItemPosition() == 1) {
                         url="http://...";
                         new YourAsyncTask().execute();
                       // connectToServerWithURL("http://...");
                    }
                }
            }).setNegativeButton("Cancel", null).show();


 class YourAsyncTask extends AsyncTask<Void, Void, Void>
    {

        private ProgressDialog progressDialog;

        @Override
        protected void onPreExecute()
        {
            //show your dialog here
            progressDialog = ProgressDialog.show(yourActivity.this,"Please wait...", "Loading  ...", true);
        }

        @Override
        protected Void doInBackground(Void... params)
        {        
            //make your request here - it will run in a different thread
            try
            {

               connectToServerWithURL(url);  

            }
            catch (Exception e)
            {
                // TODO: handle exception
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result)
        {


                progressDialog.dismiss();


        }