[英]Sharing Variable information between classes
好吧,我有我的主班
public class ViewSpotActivity extends Activity {...}
在onCreate()中,新的GetSpotDetails()。execute(); 叫做。
获取竞价详细信息如下所示:
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编程还是相当陌生。
谢谢泰勒
您应该做的就是为所需的信息创建getter方法。 首先,您应该将需要访问的变量私下存储在类中,然后为它们分配值,然后应该具有返回该数据的辅助方法。
因此,您可以执行以下操作:
private String userName;
然后将其分配到正确的位置,并进行访问:
public String getName()
{
return userName;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.