package com.example.patrice.listview02;

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


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

import android.app.ListActivity;
        import android.os.Bundle;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.ArrayAdapter;
        import android.widget.Button;
        import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

/** Note that here we are inheriting ListActivity class instead of Activity class **/
public class MainActivity extends AppCompatActivity
{

    Button btn;
    Button btnLoad;
    Button btnClr;
    EditText edit;

    /** Items entered by the user is stored in this ArrayList variable */
    ArrayList<String> list = new ArrayList<String>();
    ArrayList<String> restore_list = new ArrayList<String>();

    /** Declaring an ArrayAdapter to set items to ListView */
    ArrayAdapter<String> adapter;

    ArrayAdapter<String> arrayAdapter;

    ListView listV;

    String[] countries = new String[] {
            "India",
            "Pakistan",
            "Sri Lanka",
            "China",
            "Bangladesh",
            "Nepal",
            "Afghanistan",
            "North Korea",
            "South Korea",
            "Japan"
    };

    Object[] obj = new Object[] { "a", "b", "c" };
    ArrayList<Object> newObj = new ArrayList<Object>(Arrays.asList(obj));

    List<String> your_array_list = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.btnAdd);
        btnLoad = (Button) findViewById(R.id.btnLoad);
        btnClr = (Button) findViewById(R.id.btnClr);
        edit = (EditText) findViewById(R.id.txtItem);

        listV=(ListView)findViewById(R.id.listV);

       // adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);


        your_array_list.add("foo");
        your_array_list.add("bar");


        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, your_array_list );

/*******************/
/*
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
        newObj.add("new value");
        newObj.add("new value 2");
*/
/*******************/
        // Create a List from String Array elements
      //  final List<String> fruits_list = new ArrayList<String>(Arrays.asList(fruits));


/*
        itemList=new ArrayList<String>(Arrays.asList(items));
        adapter=new ArrayAdapter<String>(this,R.layout.list_item,R.id.txtview,itemList);
        listV=(ListView)findViewById(R.id.list);
        listV.setAdapter(adapter);
*/



        //OnClickListener listener = new OnClickListener()
        btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {

                //list.add(edit.getText().toString());
                //restore_list.add(edit.getText().toString());
                //edit.setText("");
                //adapter.notifyDataSetChanged();

                //String value = edit.getText().toString();
                //your_array_list.add(value);

                //your_array_list.add("foo");
                your_array_list.add("zorro");

                edit.setText("");
                adapter.notifyDataSetChanged();

            }
        });

        btnClr.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Toast.makeText(getApplicationContext(), "Clear listview", Toast.LENGTH_LONG).show();
                list.clear();
                adapter.notifyDataSetChanged();

            }
        });


        btnLoad.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

                for (int i = 0; i < restore_list.size(); i++)
                {
                    list.add(restore_list.get(i));
                }

                adapter.notifyDataSetChanged();

                Toast.makeText(getApplicationContext(), "Load data in listview", Toast.LENGTH_LONG).show();

            }
        });

        listV.setAdapter(adapter);

    }
}