Using Activity Methods in a Fragment

Asked

Viewed 531 times

2

I am trying to use some methods of an Activity in a Fragment, however I am not succeeding, someone can tell me if it is 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);
    }
}

Can anyone tell me if it’s possible? If so, how?

  • Why does the class updateMarker extends Activity?

  • The class updateMarker only has the method onCreate, when you say, "I intend to use all methods," you’re only referring to using the Asynctask?

  • @ramaral I’m referring to onCreate and Asynctask

  • @ramaral can tell me if I can instantiate an Asynctask ? I need to run it in another class, it is possible ?

  • My previous comment was to give you an answer explaining how to use the Asynctask in any other class but, in the face of what you said, this is not what you wanted.

  • @ramaral actually, I managed to transform my Activity into a Fragment, but I want to use the same Asynctask in other classes, can help me ?

Show 2 more comments

3 answers

2

If what you want is to use Asynctask in any class declare it in a separate file and define a constructor that receives a context and a 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");
  • Unfortunately, he’s not answering my calls. Come on, I have a class that has markers on the map, and each tag it updates the previous one with the new Retrievetask(). execute(); ! I want you to update the markers, update the amount of Asynctask markup I posted earlier.

  • 1

    What does this have to do with what you asked for? I have no idea what this is Retrievetask. Ask a new question considering we don’t know your code and we can’t read your mind. :)

1

For this, you must make one cast in getActivity()

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

This will work only if it actually UpdateMarker for a while Activity of Fragment

  • Thiago, can you tell me if I can instantiate an Asynctask ? I need to run it in another class, it’s possible ?

  • Yes, for that leave the static. To instantiate use new Updatemarker.Jsonparse();

0

Good afternoon !

You can instantiate an async task yes !
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);

  • I tried this way but he doesn’t recognize my class, so I tried Updatesfragment.Jsonparse parse = new Updatesfragment.Jsonparse(); ! But it also didn’t work, logs: Error:(100, 43) error: an enclosing instance that contains Updatesfragment.Jsonparse is required

  • The class is in a separate file ? If so, import it and see if it works.

  • He’s in another class, even though he doesn’t recognize.

  • Since the class is extending an Activity, it tries to use an Intent: Intent update = new Intent(this,updateMarker.class); startActivity(update);

Browser other questions tagged

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