所以,我有一个奇怪的问题。 我有2个活动我的工作。 其中一个活动显示一个ListView其数据通过长Extra是抓住我通过使用在database.query WHERE子句来获取结果。 在单击ListView项时,其他活动被调用,允许某人的东西添加到数据库中为ListView。
活动名称是DaysActivity.java
(使用ListView活动)和DayAddActivity.java
(即允许某人增加1天,然后在显示了活动DaysActivity.java
ListView控件)。
我遇到的问题是,当我finish()
DayAddActivity.java
它可以追溯到DaysActivity
和ListView控件仍然存在完全填充。 但是,如果我在打后退按钮DayAddActivity.java
(按钮,在我的应用程序图标的操作栏标题的左边),当它返回到DaysActivity.java,ListView控件是空的/没有了。
下面有两个代码:
DaysActivity.java:
package com.gauvion.gfit;
import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class DaysActivity extends ListActivity {
private DaysDataSource datasource;
private SimpleCursorAdapter dataAdapter;
private boolean isEditing = false;
private Toast toast_deleted;
private String[] columns = new String[] { MySQLiteHelper.COLUMN_NAME, MySQLiteHelper.COLUMN_DAY };
private int[] to;
private long routineDataID;
private String routineDataName;
@SuppressLint("ShowToast")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_days);
routineDataID = getIntent().getLongExtra("routineDataID", 0);
routineDataName = getIntent().getStringExtra("routineDataName");
setTitle(routineDataName);
toast_deleted = Toast.makeText(this, "", Toast.LENGTH_SHORT);
datasource = new DaysDataSource(this);
datasource.open();
Cursor cursor = datasource.fetchAllDays(routineDataID);
to = new int[] { R.id.listitem_day_name, R.id.listitem_day_day };
dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_day, cursor, columns, to, 0);
setListAdapter(dataAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_days, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Cursor cursor = datasource.fetchAllDays(routineDataID);
switch (item.getItemId()) {
case R.id.button_days_add:
Intent startDayAdd = new Intent(this, DayAddActivity.class);
startDayAdd.putExtra("routineDataID", routineDataID);
this.startActivity(startDayAdd);
return true;
case R.id.button_days_edit:
to = new int[] { R.id.listitem_day_edit_name, R.id.listitem_day_edit_day };
dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_day_edit, cursor, columns, to, 0);
setListAdapter(dataAdapter);
isEditing = true;
invalidateOptionsMenu();
return true;
case R.id.button_days_edit_done:
to = new int[] { R.id.listitem_day_name, R.id.listitem_day_day };
dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_day, cursor, columns, to, 0);
setListAdapter(dataAdapter);
isEditing = false;
invalidateOptionsMenu();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.button_days_edit).setVisible(!isEditing);
menu.findItem(R.id.button_days_edit_done).setVisible(isEditing);
return true;
}
@Override
protected void onResume() {
datasource.open();
Cursor cursor = datasource.fetchAllDays(routineDataID);
dataAdapter.changeCursor(cursor);
super.onResume();
}
@Override
protected void onPause() {
datasource.close();
super.onPause();
}
}
DayAddActivity.java:
package com.gauvion.gfit;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
public class DayAddActivity extends Activity {
private RoutinesDataSource datasource;
private EditText dayName;
private Spinner dayDay;
private Button saveButton;
private long routineDataID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_day_add);
routineDataID = getIntent().getLongExtra("routineDataID", 0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_day_add, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void cancelDay(View view) {
this.finish();
}
}
该cancelDay()
时点击了“取消”按钮功能被称为activity_day_add.xml
。 就像我说的,当我点击里面的“取消”按钮DayAddActivity.java
,它可以追溯到DaysActivity.java
和ListView控件是罚款。 所有的数据是存在的。 但是,如果我按在操作栏中的后退按钮为ListView(含后退箭头和活动的标题旁我的应用程序图标的按钮) DaysActivity.java
是空的/没了,标题也是空的(因为这还通过一个额外的字符串值生成)。