正在构建我的第一个Android应用程序,我有一个简单的按钮,应该获取用户的位置(并将其保存在数据库中)。单击此按钮时,我要加载的页面会加载,但在将信息保存到数据库后,它会很快消失。我确信这很简单,但对于我的生活,我无法保持新的活动。基本问题是如何在从下面的第一段代码点击按钮后,使用ACTIVE将textview保留在屏幕上。
以下是java类中用于启动活动的按钮:
public class WalkerMain extends ActionBarActivity implements View.OnClickListener{
private Button mActive;
public String user = null;
public Integer user_id = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.walker_app);
Bundle extras = getIntent().getExtras();
if (extras != null) {
user = extras.getString("user");
user_id = extras.getInt("user_id");
Log.d("In WalkerMain!", user);
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
mActive = (Button) findViewById(R.id.go_active);
// register listeners
mActive.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(this, WalkerActive.class);
i.putExtra("user", user);
i.putExtra("user_id", user_id);
startActivity(i);
}
}
更新用户信息的活动,应该只在内容视图上显示一个简单的文本框。
public class WalkerActive extends ActionBarActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener
{
private ProgressDialog pDialog;
//ids
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
public Double lat, lon;
public Integer radius = 0;
public String user = null;
public Integer user_id = null;
public JSONParser jsonParser = new JSONParser();
private static final String UPDATE_URL = "...";
private final String TAG = "APP";
private TextView mLocationView;
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.walker_active);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
protected void onStart() {
super.onStart();
// Connect the client.
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
// Disconnecting the client invalidates it.
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(Bundle bundle) {
// Display the connection status
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
if (l != null) {
lat = l.getLatitude();
lon = l.getLongitude();
}
Bundle extras = getIntent().getExtras();
if (extras != null) {
user = extras.getString("user");
user_id = extras.getInt("user_id");
Log.d("In WalkerActive", user);
}
String ret_value = null;
try {
ret_value = new UpdateActive().execute().get();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "GoogleApiClient connection has been suspend");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "GoogleApiClient connection has failed");
}
@Override
public void onLocationChanged(Location location) {
mLocationView.setText("Location received: " + location.toString());
}
class UpdateActive extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(WalkerActive.this);
pDialog.setMessage("Going Active...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user_id", user_id.toString()));
params.add(new BasicNameValuePair("radius", radius.toString()));
params.add(new BasicNameValuePair("lat", lat.toString()));
params.add(new BasicNameValuePair("lon",lon.toString()));
Log.d("request!", "starting");
Log.d("params",params.toString());
JSONObject json_update = jsonParser.makeHttpRequest(
UPDATE_URL, "POST", params);
//Log.d("completed",json.toString());
// full json_update response
Log.d("Updated Activity", json_update.toString());
// json_update success element
success = json_update.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Updated Activity!", json_update.toString());
finish();
return json_update.getString(TAG_MESSAGE);
}else{
Log.d("Registering Failure!", json_update.getString(TAG_MESSAGE));
return json_update.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
}
}
}
walker_active.xml非常简单
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="ACTIVE"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
答案 0 :(得分:3)
if (success == 1) {
Log.d("Updated Activity!", json_update.toString());
finish();
return json_update.getString(TAG_MESSAGE);
}
在这个if语句中finish();
执行的是此代码在后台运行,因此Finish();语句只是停止当前正在运行的函数并停止视图。
尝试删除Finish();声明