好的,我有我的主要课程
public class ViewSpotActivity extends Activity {...}
在onCreate()中新的GetSpotDetails()。execute();被称为。
获取Spot详细信息如下所示:
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();
}
}
}
我希望能够在spot.getString(TAG_LONG)和spot.getString(TAG_LAT)中获取信息,并在onCreate下的onClick中使用它们。有没有办法在不调用新的GetSpotDetails()。execute();的情况下执行此操作。很抱歉,如果这是一个简单的答案,我对Android上的编程相当新。
谢谢你, 泰勒
答案 0 :(得分:0)
您应该做的是为您想要的信息创建getter方法。首先,您应该在类中私有地存储您需要访问的变量,然后将值分配给它们,然后您应该有返回该数据的辅助方法。
所以你可以这样做:
private String userName;
然后将其分配到正确的位置,并访问它:
public String getName()
{
return userName;
}