onClick on按钮时不显示alertDialog

时间:2015-06-27 17:52:22

标签: android android-asynctask alertdialog getjson

我是Android应用开发的初学者。我现在正在创建一个求职应用程序,允许用户输入关键字来搜索工作。如果搜索后没有结果,我会尝试显示一个对话框,说明找不到结果,并要求用户再次搜索。然后,即使没有任何搜索结果,也不会出现对话框。当你引用doInBackground()时,有Log.d(“Response:”,“>”+ jsonStr);所以有两种情况:

第一种情况(有搜索结果):

响应::> {“info”:[{“INSIDE HERE THE JOB INFORMATION”}

第二种情况(搜索后没有结果):

响应::> {“success”:0,“message”:“找不到信息”}

我的逻辑是,如果搜索后没有结果,则会出现alertDialog以提醒用户再次搜索。我在私有类的doInBackground()中实现了这个部分GetContacts扩展了AsyncTask。你能看一看并亲切帮忙吗?谢谢!

MainActivityJsonParsing.java

public class MainActivityJsonParsing extends ListActivity {


List<NameValuePair> params = new ArrayList<NameValuePair>();
String PostNameInputByUser;
String LocationInputByUser;

private ProgressDialog pDialog;
final Context context = this;

// URL to get contacts JSON
private static String url = "http://192.168.0.102/get_json.php";

// JSON Node names
private static final String TAG_INFO = "info";
private static final String TAG_POSTNAME = "PostName";
private static final String TAG_LOCATION = "Location";
private static final String TAG_SALARY = "Salary";
private static final String TAG_RESPONSIBILITY = "Responsibility";
private static final String TAG_COMPANY = "Company";
private static final String TAG_CONTACT = "Contact";

// contacts JSONArray
JSONArray infos = null;

// Hashmap for ListView
ArrayList<HashMap<String, String>> infoList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_json_parsing);



    infoList = new ArrayList<HashMap<String, String>>();
    Intent intent = getIntent();
    PostNameInputByUser = intent.getStringExtra("PostName");
    LocationInputByUser = intent.getStringExtra("Location");

    params.add(new BasicNameValuePair("PostName", PostNameInputByUser));
    params.add(new BasicNameValuePair("Location", LocationInputByUser));

    final ListView lv = getListView();

    // Listview on item click listener
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.PostName))
                    .getText().toString();
            String cost = ((TextView) view.findViewById(R.id.Location))
                    .getText().toString();
            String description = ((TextView) view.findViewById(R.id.Salary))
                    .getText().toString();

            HashMap<String, String> info = new HashMap<String, String>();
            info = (HashMap<String, String>) lv.getAdapter().getItem(position);


            // Starting single contact activity
            Intent in = new Intent(getApplicationContext(),
                    SingleJobActivity.class);

            in.putExtra(TAG_POSTNAME, name);
            in.putExtra(TAG_LOCATION, cost);
            in.putExtra(TAG_SALARY, description);
            in.putExtra(TAG_RESPONSIBILITY, info.get(TAG_RESPONSIBILITY));
            in.putExtra(TAG_COMPANY, info.get(TAG_COMPANY));
            in.putExtra(TAG_CONTACT, info.get(TAG_CONTACT));

            startActivity(in);

        }
    });
    // Calling async task to get json
    new GetContacts().execute();
}

/**
 * Async task class to get json by making HTTP call
 * */
private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MainActivityJsonParsing.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();



    }

    @Override
    protected Void doInBackground(Void... arg0) {

        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET, params);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != "{" + "\"" + "success" + "\"" + ":0," + "\"" + "message"+ "\"" + ":" + "\"" +  "No info found" + "\"" +  "}") {

            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                infos = jsonObj.getJSONArray(TAG_INFO);
                // looping through All Contacts
                for (int i = 0; i < infos.length(); i++) {
                    JSONObject c = infos.getJSONObject(i);

                    String id = c.getString(TAG_POSTNAME);
                    String name = c.getString(TAG_LOCATION);
                    String email = c.getString(TAG_SALARY);
                    String address = c.getString(TAG_RESPONSIBILITY);
                    String gender = c.getString(TAG_COMPANY);
                    String mobile = c.getString(TAG_CONTACT);


                    // tmp hashmap for single contact
                    HashMap<String, String> info = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    info.put(TAG_POSTNAME, id);
                    info.put(TAG_LOCATION, name);
                    info.put(TAG_SALARY, email);
                    info.put(TAG_RESPONSIBILITY, address);
                    info.put(TAG_COMPANY, gender);
                    info.put(TAG_CONTACT, mobile);
                    // adding contact to contact list
                    infoList.add(info);
                }
            } catch (JSONException e) {
                e.printStackTrace();

            }
        } else  {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
            alertDialogBuilder.setTitle("Job Search Result");
            alertDialogBuilder.setMessage("No jobs found !");
            alertDialogBuilder.setCancelable(false);
            alertDialogBuilder.setPositiveButton("Search Again", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Intent intent = new Intent(context, MainActivity.class);
                    startActivity(intent);
                }
            });
            alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id){
                    dialog.cancel();
                }
            });

            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }


        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();


        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(
                MainActivityJsonParsing.this, infoList,


                R.layout.list_item_json_parsing, new String[] { TAG_POSTNAME, TAG_LOCATION,
                TAG_SALARY }, new int[] { R.id.PostName,
                R.id.Location, R.id.Salary });

        setListAdapter(adapter);

    }
}

ServiceHandler.java

public class ServiceHandler {

static String response = null;
public final static int GET = 1;
public final static int POST = 2;

public ServiceHandler() {

}

/**
 * Making service call
 * @url - url to make request
 * @method - http request method
 * */
public String makeServiceCall(String url, int method) {
    return this.makeServiceCall(url, method, null);
}

/**
 * Making service call
 * @url - url to make request
 * @method - http request method
 * @params - http request params
 * */
public String makeServiceCall(String url, int method,
                              List<NameValuePair> params) {
    try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            // adding post params
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }

            httpResponse = httpClient.execute(httpPost);

        } else if (method == GET) {
            // appending params to url
            if (params != null) {
                String paramString = URLEncodedUtils
                        .format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);

            httpResponse = httpClient.execute(httpGet);

        }
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return response;

}
}

2 个答案:

答案 0 :(得分:0)

!doInBackground方法在UI线程下执行,因此任何UI更改都不会反映在UI中。要使对话框显示,您需要在onProgressUpdateonPostExecute中的UI线程上执行此操作:

/**
 * Async task class to get json by making HTTP call
 **/
private class GetContacts extends AsyncTask<Void, Void, Boolean> {

....

@Override
protected Boolean doInBackground(Void... arg0) {

    // Creating service handler class instance
    ServiceHandler sh = new ServiceHandler();

    // Making a request to url and getting response
    String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET, params);

    Log.d("Response: ", "> " + jsonStr);

    //really should be using the JSONObject or regex here instead of concatenating a bunch of strings.
    boolean result = ! jsonStr.equals("{" + "\"" + "success" + "\"" + ":0," + "\"" + "message" + "\"" + ":" + "\"" + "No info found" + "\"" + "}");

    if (result) {
        try {
            JSONObject jsonObj = new JSONObject(jsonStr);

            // Getting JSON Array node
            infos = jsonObj.getJSONArray(TAG_INFO);
            // looping through All Contacts
            for (int i = 0; i < infos.length(); i++) {
                JSONObject c = infos.getJSONObject(i);

                String id = c.getString(TAG_POSTNAME);
                String name = c.getString(TAG_LOCATION);
                String email = c.getString(TAG_SALARY);
                String address = c.getString(TAG_RESPONSIBILITY);
                String gender = c.getString(TAG_COMPANY);
                String mobile = c.getString(TAG_CONTACT);


                // tmp hashmap for single contact
                HashMap<String, String> info = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                info.put(TAG_POSTNAME, id);
                info.put(TAG_LOCATION, name);
                info.put(TAG_SALARY, email);
                info.put(TAG_RESPONSIBILITY, address);
                info.put(TAG_COMPANY, gender);
                info.put(TAG_CONTACT, mobile);
                // adding contact to contact list
                infoList.add(info);
            }
        } catch (JSONException e) {
            e.printStackTrace();

        }
    }
    return result;
}

@Override
protected void onPostExecute(Boolean foundResults) {
    super.onPostExecute(foundResults);
    if (foundResults) {

        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(
                MainActivityJsonParsing.this, infoList,
                R.layout.list_item_json_parsing, new String[]{TAG_POSTNAME, TAG_LOCATION,
                TAG_SALARY}, new int[]{R.id.PostName,
                R.id.Location, R.id.Salary});

        setListAdapter(adapter);
    } else {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder.setTitle("Job Search Result");
        alertDialogBuilder.setMessage("No jobs found !");
        alertDialogBuilder.setCancelable(false);
        alertDialogBuilder.setPositiveButton("Search Again", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Intent intent = new Intent(context, MainActivity.class);
                startActivity(intent);
            }
        });
        alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }

    // Dismiss the progress dialog
    if (pDialog.isShowing()) {
        pDialog.dismiss();
    }
}

答案 1 :(得分:0)

以上答案是正确的。另一种方法是在MainActivity中定义一个处理程序,并将处理程序引用作为参数传递给Async任务的构造函数。然后从doInBackGround()通过处理程序发送消息。你可以通过它显示对话框! 希望它有所帮助!

    class MyActivity extends Activity{ 

            private Handler handler = null;

            public void onCreate(){
                handler = new Handler( new Handler.Callback(){

                    @Override
                    public boolean handleMessage(final Message msg) {

                     if(msg.what == 0){
                        showDialog();
                     } else{
                          removeDialog();
                      }
                  );
            }

public SomeTask extends AsyncTask{

Handler handler;
public SomeTask(Handler handler){
    this.handler = handler;
}

public void doInBackground(){
   //do your work
  if(search results in zero)
     handler.sendEmptyMessage(0);
  else
     handler.sendEmptyMessage(1);

} }