Taking an attribute from a Class

Asked

Viewed 43 times

1

My class asks for a context as a parameter (for example: MainActivity). I can leave without the context? just passing the query as parameter...

Is this way:

public HttpTask(Context context, String query)
    {
        super();
        this.context = context;
        mHttpRequest = new HttpRequest(context, "https://services-dev.redetendencia.com.br", query);
    }

    @Override
    protected Long doInBackground(String... params)
    {

        result = mHttpRequest.buscarSql();
        return null;
    }

    @Override
    protected void onPostExecute(Long aLong)
    {
        super.onPostExecute(aLong);
        try {
            ((Interface) context).onQueryTaskExecute(result);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public interface Interface {
        void onQueryTaskExecute(String result) throws JSONException;
    }
  • Related: https://answall.com/q/252765/64969

1 answer

0

You can allow the class to be instantiated without requiring the context by creating another constructor with only the String query as parameter. Now you will not be able to use any internal method that requires context.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.