-2

I have the following Utils class and I want to write a friendly toast to my users in the catch block like this:

catch (JSONException e){
  Log.e(LOG_TAG, "String to JSON failed: " + e);
  showToast(getActivity, "Test toast");
}

But the problem is how do I get my activity or application context? I tried this https://stackoverflow.com/a/23423970 but it doesn't work for me because I'm not sure where to get my context from? If I have to create a constructor of the Util class then it defeats the purpose of the Util class as I dont want to instantiate this class.

Here's my class:

public class Utils {
  public static boolean showPercent = true;

  public static ArrayList quoteJsonToContentVals(String JSON){
    ArrayList<ContentProviderOperation> batchOperations = new ArrayList<>();
    JSONObject jsonObject = null;
    JSONArray resultsArray = null;
    try{
      jsonObject = new JSONObject(JSON);
      if (jsonObject != null && jsonObject.length() != 0){
        jsonObject = jsonObject.getJSONObject("query");
        int count = Integer.parseInt(jsonObject.getString("count"));
        if (count == 1){
          jsonObject = jsonObject.getJSONObject("results")
              .getJSONObject("quote");
          batchOperations.add(buildBatchOperation(jsonObject));
        } else{
          resultsArray = jsonObject.getJSONObject("results").getJSONArray("quote");

          if (resultsArray != null && resultsArray.length() != 0){
            for (int i = 0; i < resultsArray.length(); i++){
              jsonObject = resultsArray.getJSONObject(i);
              batchOperations.add(buildBatchOperation(jsonObject));
            }
          }
        }
      }
    } catch (JSONException e){
      Log.e(LOG_TAG, "String to JSON failed: " + e);
      showToast(getActivity, "Test toast");
    }
    return batchOperations;
  }

  public static void showToast(Context context, String text) {
    Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}
  .....
Community
  • 1
  • 1
M Garp
  • 195
  • 1
  • 15

3 Answers3

1

To do anything related with UI you need to have context so you can't display Toast without it.

So the solution is you can create an Application class and add a static context their and use it when ever you want .

Steps :

1.) create a class and extends Application class

 class App extends Application{
  }

2.) register your class in manifest application tag

   <application android:name="com.yourpackage.App">

3.) create a static context holder and static function

   private static Context context;
   public static Context getAppContext() {
        return context;
   }

4.) use the static function to get the context

Follow this link to see the coding example

So eventually your toast function will look like

 public static void showToast(String text) {
    Toast.makeText(App.getAppContext(), text, Toast.LENGTH_LONG).show();
}

Or

The other option is pass your Activity context to this quoteJsonToContentVals function (if you can) and make a context holder in your class and use it.

Community
  • 1
  • 1
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
1

Try out this.

    public static ArrayList quoteJsonToContentVals(Activity activity, String JSON){
        ArrayList<ContentProviderOperation> batchOperations = new ArrayList<>();
        JSONObject jsonObject = null;
        JSONArray resultsArray = null;
        try{
          jsonObject = new JSONObject(JSON);
          if (jsonObject != null && jsonObject.length() != 0){
            jsonObject = jsonObject.getJSONObject("query");
            int count = Integer.parseInt(jsonObject.getString("count"));
            if (count == 1){
              jsonObject = jsonObject.getJSONObject("results")
                  .getJSONObject("quote");
              batchOperations.add(buildBatchOperation(jsonObject));
            } else{
              resultsArray = jsonObject.getJSONObject("results").getJSONArray("quote");

              if (resultsArray != null && resultsArray.length() != 0){
                for (int i = 0; i < resultsArray.length(); i++){
                  jsonObject = resultsArray.getJSONObject(i);
                  batchOperations.add(buildBatchOperation(jsonObject));
                }
              }
            }
          }
        } catch (JSONException e){
          Log.e(LOG_TAG, "String to JSON failed: " + e);
          showToast(activity, "Test toast");
        }
        return batchOperations;
      }
public static void showToast(Activity activity, String text) {
    Toast.makeText(activity, text, Toast.LENGTH_LONG).show();
Alex Chengalan
  • 8,211
  • 4
  • 42
  • 56
  • thanks Im not in front of my Android Studio right now. will try it later – M Garp Nov 01 '16 at 13:26
  • that did not work because the class that is calling this method is GcmTaskService and I don't know how to pass in my activity: Utils.quoteJsonToContentVals(getResponse, What do I put here); – M Garp Nov 02 '16 at 23:19
  • The service itself a context, so just pass the GcmTaskService.this as context instead of Activity – Alex Chengalan Nov 03 '16 at 06:08
1

Simply pass the context (not the activity, because the context is enough) to your quoteJsonToContentVals() method.

public static ArrayList quoteJsonToContentVals(Context context, String JSON){

   ...

      showToast(context, "Test toast");

   ...

}

public static void showToast(Context context, String text) {
   Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}

Btw. Why are you not using gson (https://github.com/google/gson) for your json mapping?

André Roß
  • 279
  • 3
  • 5