Search code examples
androidandroid-asynctaskandroid-alertdialoggetjson

alertDialog not showing when onClick on a button


I'm a beginner in android app development. I'm now creating a job search app which allows users to input keywords to search jobs. If there is no result after search, I attempt to show a dialog saying that there is not result found and ask the users to search again. Howerver, the dialog doesn't appear even if there is no any search result. As you refer to doInBackground(), there is Log.d("Response: ", "> " + jsonStr); So there are 2 cases:

1st case (have search result):

Response:﹕ > {"info":[{"INSIDE HERE IS THE JOB INFORMATION"}

2nd case (no result after search):

Response:﹕ > {"success":0,"message":"No info found"}

My logic is that if there is no result after search, an alertDialog appears to remind users to search again. I implement this part inside doInBackground() of private class GetContacts extends AsyncTask. Can you have a look and kindly help ? Thank you !

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;

}
}

Solution

  • !The doInBackground method is executed off the UI thread so any UI changes won't reflect in the UI. To make your dialog show, you will need to do that on the UI thread in onProgressUpdate or onPostExecute:

    /**
     * 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();
        }
    }