package com.example.patrice.listview04;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;




import java.util.ArrayList;
        import java.util.List;
        import android.os.Bundle;
        import android.app.Activity;
        import android.view.Menu;
        import android.view.View;
        import android.widget.ArrayAdapter;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    List<String> li;
    ListView list;

    ArrayAdapter<String> adp;



    private static final String STATE_ITEMS = "items";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        li=new ArrayList<String>();
        li.add("List 1");
        li.add("List 2");
        li.add("List 3");

        final Button show=(Button) findViewById(R.id.button1);
        final EditText et=(EditText) findViewById(R.id.editText1);
        list=(ListView) findViewById(R.id.listView1);

        add();

        show.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                li.add(et.getText().toString());
                et.setText(null);
                add();
            }
        });
    }

    public void add()
    {
        adp = new ArrayAdapter<String>(getBaseContext(),R.layout.list,li);
        list.setAdapter(adp);
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(STATE_ITEMS, adapter.getList());
    }

}