Take an attribute from a Class

1

My class asks for a context (for example, MainActivity ) as a parameter. Can I leave without context? just passing as a parameter to query ...

It looks like this:

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;
    }
    
asked by anonymous 05.03.2018 / 13:57

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 a parameter. Now you can not use any internal methods that require context.

    
05.03.2018 / 20:24