2014-01-17 118 views
0

好了,所以我有我的主类共享类之间的可变信息

public class ViewSpotActivity extends Activity {...} 

在的onCreate()新GetSpotDetails()执行()。叫做。

获取现货细节是这样的:

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)的信息,并利用它们在一个onClick是下的onCreate。有没有办法做到这一点无需召回新GetSpotDetails()执行();对不起,如果这是一个简单的答案我是相当新的android编程。

谢谢 泰勒

回答

0

你应该做的是创造你想要的信息获取方法。首先,你应该有你需要访问私人存储在类中的变量,然后分配值给他们,那么你应该有辅助方法,返回的数据。

所以,你可以这样做:

private String userName; 

,然后将其分配到正确的位置,并对其进行访问:

public String getName() 
    { 
     return userName; 
    } 
+0

所以这是一个非常简单的解决方案哈哈。谢谢。 – TylerM

+0

没问题,很高兴我能帮助。 –