按下后退按钮时,列表视图中的新条目将消失



我有一个listview,它初始化的条目很少。在menu栏中,我可以选择添加更多打开dialog的条目,并且必须输入三个字段。单击save按钮时,条目将显示在listview中,但在按下后退按钮并重新启动应用程序后,将显示较旧的listview,即不显示listview中新输入的值。

主代码:

public class SecondScreen extends ListActivity implements AppCompatCallback,AdapterView.OnItemClickListener {
private Toolbar bar;
private AppCompatDelegate delegate;
List<Model> list;
Model selectedModel;
AGSQLiteHelper db;
Adapter adapter;
private EditText task_Title,task_Description,task_Date;
@Override
protected void onCreate(Bundle savedInstanceState) {
delegate=AppCompatDelegate.create(this,this);
super.onCreate(savedInstanceState);
//savedInstanceState.putBoolean("mIsBackButtonPressed",mIsBackButtonPressed);
delegate.setContentView(R.layout.activity_second_screen);
bar=(Toolbar)findViewById(R.id.toolbarID);
delegate.setSupportActionBar(bar);
ActionBar ab=delegate.getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
db=new AGSQLiteHelper(this);
db.onUpgrade(db.getWritableDatabase(),1,2);
db.createTask(new Model("abcde","description abcde","19/11/12"));
db.createTask(new Model("bcdese","description bcdese","18/12/12"));
db.createTask(new Model("mnbv","description mnbv","2/11/10"));
db.createTask(new Model("poiuy","description poiuy","1/5/6"));
list=db.getAllTasks();
adapter=new Adapter(this,R.layout.row,list);
getListView().setOnItemClickListener(this);
setListAdapter(adapter);
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Nullable
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
return super.onCreateView(name, context, attrs);
}
@Override
public void onSupportActionModeStarted(ActionMode mode) {
}
@Override
public void onSupportActionModeFinished(ActionMode mode) {
}
@Nullable
@Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
return null;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.add){
Toast.makeText(getApplicationContext(),"Add was clicked",Toast.LENGTH_LONG).show();
final Dialog dialog=new Dialog(SecondScreen.this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Dialog");
Button save_button=(Button)dialog.findViewById(R.id.saveButtonID);
Button cancel_button=(Button)dialog.findViewById(R.id.cancelButtonID);
task_Title=(EditText)dialog.findViewById(R.id.titleEditTextID);
task_Description=(EditText)dialog.findViewById(R.id.descriptionEditTextID);
task_Date=(EditText)dialog.findViewById(R.id.dateEditTextID);
dialog.show();
save_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedModel=new Model();
selectedModel.setTask_title(task_Title.getText().toString());
selectedModel.setTask_description(task_Description.getText().toString());
selectedModel.setTask_date(task_Date.getText().toString());
db.insertTask(selectedModel);
//Toast.makeText(getApplicationContext(),"model ="+selectedModel.getTask_title(),Toast.LENGTH_LONG).show();
//Log.i("Second Screen","list length before "+list.size());
//list=db.getAllTasks();
list.add(selectedModel);
// Log.i("Second Screen","list length after "+list.size());
// Adapter adapter =( Adapter)getListView().getAdapter();
//adapter=new Adapter(this,R.layout.row,list);
adapter.notifyDataSetChanged();
//Toast.makeText(getApplicationContext(),"model ="+selectedModel.getTask_title(),Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
cancel_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
if(item.getItemId()==R.id.done){
Toast.makeText(getApplicationContext(),"Done was clicked",Toast.LENGTH_LONG).show();
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}

}

有什么建议吗?

实际上,在android中,如果你想存储值,你必须使用SharedPreference,Sqlite数据库的数据存储在web中,你可以使用MySQl。

现在实际上,当您在列表视图中添加新项时,存储在本地内存中的项,并且正在使用 datamodel 来管理这些数据,当您终止应用程序时,本地内存被清除,当您再次运行应用程序时,应用程序没有这些数据,这就是应用程序不在列表视图中显示项目的原因。如果您想存储这些数据,请使用我上面讨论的数据库。

相关内容

  • 没有找到相关文章

最新更新