At the press of back button the new entries in the ListView are gone

93 views Asked by At

I have a listview which is initialized with few entries. In the menu bar I have an option for adding more entries for which a dialog is opened and I have to enter three fields. On clicking the save button the entries are displayed in the listview but on pressing the back button and after restarting the app the older listview is displayed ,i.e, the newly entered values in the listview are not displayed.

The main code :

   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;
    }


}

Any suggestions?

1

There are 1 answers

3
Rishabh Mahatha On

Actually, in android if you want to store the value you have to use SharedPreference, Sqlite database of for storing the data in web you can use MySQl.

Now come to your problem actually when you add a new item in the list view the item stored in local memory and you are using datamodel to manage those data and when you kill the application, the local memory gets cleared and when you again run your application, the application does not have those data that is why the application is not showing items in list view. If you want to store those data use database as I discussed above.