在我的活动中,我有ActionBar,其中包含一个新的动作按钮(在ListView上添加一个新项目)和一个ListView。问题是,在我添加一些项目然后按下后退按钮后,ListView将返回上一个ListView并减少项目,并在一两秒后显示包含所有项目的完整列表视图。我认为这个问题类似于listview与之前的重复。 这是我的活动:
public class ListNotesActivity extends ActionBarActivity {
private ArrayList<Note> notes;
private Database db;
private long lastBackPressTime = 0;
private Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_notes);
notes = new ArrayList<Note>();
db = new Database(this);
getNotes();
ActionBar ab = getSupportActionBar();
ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#E89619")));
ab.setTitle("Your notes");
}
@Override
public void onBackPressed() {
if (this.lastBackPressTime < System.currentTimeMillis() - 4000) {
toast = Toast.makeText(this, "Press back again to exit", 4000);
toast.show();
this.lastBackPressTime = System.currentTimeMillis();
} else {
if (toast != null)
toast.cancel();
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list_notes, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.action_new:
//In this activity that I start, It will save the new Item in the database and then it starts this activity again that will read the database with the new item.
Intent i = new Intent(ListNotesActivity.this,
NotesActivity.class);
i.putExtra(NEWNOTE, true);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void getNotes() {
final Toast t = Toast.makeText(this, R.string.retrieving_notes,
Toast.LENGTH_SHORT);
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
notes = db.getNotes();
Log.v("MyActivity",
"Read notes on the database: " + notes.size());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
populateList();
}
};
t.show();
task.execute();
}
public void populateList() {
NoteAdapter adapter = new NoteAdapter(this, notes);
final ListView listView = (ListView) findViewById(R.id.list_notes);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parentView, View childView,
int position, long id) {
Note note = notes.get(position);
Log.v("MyActivity",
"LISTNOTESACTIVITY: note's title: " + note.getTitle());
Bundle b = new Bundle();
b.putSerializable(EDITNOTE2, note);
Intent i = new Intent(ListNotesActivity.this,
NotesActivity.class);
i.putExtra(EDITNOTE, true);
i.putExtra(EDITNOTE2, b);
startActivity(i);
}
});
}
我希望我能说清楚。 提前谢谢!
答案 0 :(得分:1)
尝试从getNotes()方法中删除此行
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
答案 1 :(得分:1)
您的列表视图仅在创建活动时更新(而不是在恢复活动时),因为您只在onCreate()中调用getNotes()。在方法getNotes()中,您进行异步操作。
如果您想立即更新列表视图,请采取这样的策略。
在创建新项目后的NotesActivity中添加并调用此方法
private void saveItem(Note note)
{
Intent data = new Intent();
// set all data from your item
data.putExtra(name, value);
setResult(RESULT_OK, data);
}
在ListNotesActivity中,您应该使用此调用启动新活动
startActivityForResult(intent, 1);
并覆盖此方法
@Override
protected void onResume()
{
super.onResume();
// This call on your list view to update rows
listViewAdapter.notifyDataSetChanged();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode == RESULT_OK && requestCode == 1)
{
Note note = new Note();
// Extract your data from intent
data.getExtra(name); // and set to note variable
// add your new item to list view adapter
listViewAdapter.addNewItem(note);
}
}
我希望这会对你有所帮助。