Use methods of an Activity in a Fragment

2

I'm trying to use some methods of an Activity in a Fragment, but I can not, can someone tell me if it's possible?

public class updateMarker extends Activity {
    TextView uid;

    //URL to get JSON Array
    private static String url = "http://t4web.hospedagemdesites.ws/alarme/update.php";

    //JSON Node Names
    private static final String TAG_USER = "markers";
    private static final String TAG_ID = "id";


    JSONArray markers = null;

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


        new JSONParse().execute();
    }

    public class JSONParse extends AsyncTask<String, String, JSONObject> {
        private ProgressDialog pDialog;
        @Override
        public void onPreExecute() {
            super.onPreExecute();
            uid = (TextView)findViewById(R.id.uid);
            pDialog = new ProgressDialog(updateMarker.this);
            pDialog.setMessage("Carregando..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

        }

        @Override
        protected JSONObject doInBackground(String... args) {
            JSONParser jParser = new JSONParser();

            // Getting JSON from URL
            JSONObject json = jParser.getJSONFromUrl(url);
            return json;
        }
        @Override
        protected void onPostExecute(JSONObject json) {
            pDialog.dismiss();
            try {
                // Getting JSON Array
                markers = json.getJSONArray(TAG_USER);
                JSONObject c = markers.getJSONObject(0);

                // Storing  JSON item in a Variable
                String id = c.getString(TAG_ID);


                //Set JSON Data in TextView
                uid.setText(id);

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }

}

I intend to use all methods of this class in this Fragment:

public class UpdatesFragment extends Fragment {


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.updates_layout, null);
    }
}

Does anyone know if it's possible? If so, how?

    
asked by anonymous 17.12.2015 / 12:47

3 answers

2

If you want to be able to use AsyncTask in any class declare it in a separate file and define a constructor that receives a context and listener where the result will be passed.

public class MyParser extends AsyncTask<String, String, JSONObject> {

    //Interface que o Listener deve implementar
    public interface OnResultListener {
        void onResult(String id);
    }

    //JSON Node Names
    private static final String TAG_USER = "markers";
    private static final String TAG_ID = "id";

    private ProgressDialog pDialog;
    private Context context;
    private onResultListener listener;

    public MyParser(Context context, OnResultListener listener){

        this.context = context;
        this.listener = listener;
    }

    @Override
    public void onPreExecute() {
        super.onPreExecute();

        pDialog = new ProgressDialog(context);
        pDialog.setMessage("Carregando..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();

    }

    @Override
    protected JSONObject doInBackground(String... args) {
        JSONParser jParser = new JSONParser();

        String url = args[0];
        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);
        return json;
    }
    @Override
    protected void onPostExecute(JSONObject json) {
        pDialog.dismiss();
        try {
            // Getting JSON Array
            JSONArray markers = json.getJSONArray(TAG_USER);
            JSONObject c = markers.getJSONObject(0);

            // Storing  JSON item in a Variable
            String id = c.getString(TAG_ID);


            //Chama o callback com o id
            if(listener != null){
                listener.onResult(id);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}  

To use:

MyParser parser = new MyParser(context, new MyParser.OnResultListener() {

    @Override
    public void onResult(String id) {

        //Utilize o id como entender.
    }
});

parser.execute("http://t4web.hospedagemdesites.ws/alarme/update.php");
    
17.12.2015 / 16:20
1

For this, you should make a cast in getActivity()

 UpdateMarker updateMarker = ((UpdateMarker)getActivity());

This will only work if really UpdateMarker is Activity of Fragment

    
17.12.2015 / 12:56
0

Good afternoon!

You can instantiate an async task sim! First you use a constructor to instantiate: JSONParse parse = new JSONParse ();

Then you run asynctask with the parameters you declared to be passed: parse.execute(var);

    
17.12.2015 / 17:23