Okay, So i have my main class
public class ViewSpotActivity extends Activity {...}
In onCreate() new GetSpotDetails().execute(); is called.
Get Spot details looks like this:
class GetSpotDetails extends AsyncTask<String, String, JSONObject> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ViewSpotActivity.this);
pDialog.setMessage("Loading Spot details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* Getting details in background thread
* */
protected JSONObject doInBackground(String... String) {
JSONObject spot = null;
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("pid", pid));
// getting details by making HTTP request
// Note that details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
url_detials, "GET", params);
// check your log for json response
Log.d("Single Spot Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray spotObj = json
.getJSONArray(TAG_SPOT); // JSON Array
// get first product object from JSON Array
int value = Integer.parseInt(pid);
int n=0;
while(Integer.parseInt(spotObj.getJSONObject((n)).getString(TAG_PID))!=value){
n++;
}
spot = spotObj.getJSONObject((n));
}else{
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
return spot;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(JSONObject spot) {
if (spot != null) {
setContentView(R.layout.view_spot);
// pid found
// Edit Text
txtName = (TextView) findViewById(R.id.outputName);
txtLong = (TextView) findViewById(R.id.outputLong);
txtLat = (TextView) findViewById(R.id.outputLat);
txtPavement = (TextView) findViewById(R.id.outputPavement);
txtTraffic = (TextView) findViewById(R.id.outputTraffic);
txtEnvironment = (TextView) findViewById(R.id.outputEnvironment);
//need to add rest...
// display data in Text
try {
//need to add rest...
txtName.setText("Spot Name: " + spot.getString(TAG_NAME));
txtLong.setText("Longitude: " + spot.getString(TAG_LONG));
txtLat.setText("Latitude: " + spot.getString(TAG_LAT));
txtPavement.setText("Pavement: " + spot.getString(TAG_PAVEMENT));
txtTraffic.setText("Traffic: " + spot.getString(TAG_TRAFFIC));
txtEnvironment.setText("Environment: " + spot.getString(TAG_ENVIRONMENT));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
}
I want to be able to get the information in spot.getString(TAG_LONG) and spot.getString(TAG_LAT) and use them in an onClick that is under onCreate. Is there any way to do this without recalling new GetSpotDetails().execute();. Sorry if it's a simple answer I'm fairly new to programming on android.
Thank you, Tyler
What you should do is create getter methods for the information you want. First of all you should have the variables that you need to access stored privately in the class, and then assign the values to them, and then you should have helper methods which return that data.
So you could do something like:
private String userName;
and then assign it to the right location, and to access it:
public String getName()
{
return userName;
}