package com.example.patrice.listview01;

import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import android.widget.AdapterView.OnItemClickListener;


public class MainActivity extends AppCompatActivity
{
    ArrayAdapter<String> adapter;
    EditText editText;
    ArrayList<String> itemList;

    ListView mListInstanceState;

    ListView listV;

    Parcelable state;

    private static final String STATE_LIST = "State Adapter Data";

    private static final String STATE_ITEMS = "items";


    private Parcelable mListState = null;
    private static final String LIST_STATE_KEY = "liststate";

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        listV=(ListView)findViewById(R.id.list);
        editText=(EditText)findViewById(R.id.txtInput);

        if (savedInstanceState != null)
        {
            itemList = (ArrayList<String>) savedInstanceState.getSerializable(STATE_LIST);
        }
        else
        {
            //itemList = new ArrayList<>();
            String[] items={"Apple","Banana","Clementine"};
            itemList = new ArrayList<String>(Arrays.asList(items));
        }

        adapter=new ArrayAdapter<String>(this,R.layout.list_item,R.id.txtview,itemList);
        listV.setAdapter(adapter);

        Button btAdd=(Button)findViewById(R.id.btAdd);

        btAdd.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                String newItem=editText.getText().toString();
                // add new item to arraylist
                itemList.add(newItem);
                // notify listview of data changed
                adapter.notifyDataSetChanged();
            }

        });

        listV.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                //items

                //Toast.makeText(MainActivity.this, itemList.get(position), Toast.LENGTH_SHORT).show();

                String value = listV.getItemAtPosition(position).toString();
                int pos = parent.getPositionForView(view);
                Toast.makeText(MainActivity.this ,pos + "",Toast.LENGTH_SHORT).show();

            }
        });

    }


    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        outState.putSerializable(STATE_LIST, itemList);
    }



/*
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList(STATE_LIST, getAdapter().getList());
    }
*/
/*
    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        //outState.putParcelable(STATE_LIST, listV.onSaveInstanceState());
        Parcelable state = listV.onSaveInstanceState();
    }
*/
/*
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Save list state in the bundle so we can restore it after the QueryHandler has run

        outState.putParcelableArrayList("key", itemList);
        super.onSaveInstanceState(outState);



        if (listV != null) {
            outState.putParcelable(LIST_STATE_KEY, listV.onSaveInstanceState());

            outState.putSerializable(STATE_ITEMS, adapter.getList());

        }
    }
*/
/*
    @Override
    protected void onRestoreInstanceState(Bundle icicle) {
        super.onRestoreInstanceState(icicle);
        // Retrieve list state. This will be applied after the QueryHandler has run
        mListState = icicle.getParcelable(LIST_STATE_KEY);
    }
*/
/*
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        //listV.onRestoreInstanceState((Parcelable) mListInstanceState);
        listV.onRestoreInstanceState(state);
    }
*/
}