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?
– ramaral
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
@ramaral I’m referring to onCreate and Asynctask
– tharley carvalho
@ramaral can tell me if I can instantiate an Asynctask ? I need to run it in another class, it is possible ?
– tharley carvalho
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
@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 ?
– tharley carvalho
Let’s go continue this discussion in chat.
– tharley carvalho