package com.example.patrice.http01;


import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

//https://androidzoo.wordpress.com/2011/10/28/working-with-listview-in-android-customize-listview-add-item-via-a-button-click-and-also-clickable-each-button-in-each-row/

// http://www.worldbestlearningcenter.com/tips/Android-SQLite-delete-row.htm

//  https://examples.javacodegeeks.com/android/core/ui/listview/android-multitouch-listview-example/

// https://www.android-examples.com/remove-all-items-from-listview-in-android-programmatically/

// http://androidforbegineers.blogspot.fr/2013/08/delete-row-item-in-listview-android.html

// http://www.android--tutorials.com/2016/10/android-add-remove-listview-item.html

// http://www.prandroid.com/2016/03/dynamic-add-and-remove-item-on-listview.html   +++++ a voir le plus tot possible

//http://wptrafficanalyzer.in/blog/dynamically-add-items-to-listview-in-android/

// http://www.androidbegin.com/tutorial/android-delete-multiple-selected-items-listview-tutorial/

// http://www.sunilandroid.com/2012/12/listview-with-multiple-choice.html   multichoice ++++


public class listview01 extends AppCompatActivity
{
    ListView listView;
    EditText editTextView;
    ArrayList<Model> ItemModelList;
    CustomAdapter customAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_item01);

        listView = (ListView) findViewById(R.id.listview);
        editTextView = (EditText) findViewById(R.id.editTextView);

        ItemModelList = new ArrayList<Model>();
        customAdapter = new CustomAdapter(getApplicationContext(), ItemModelList);

        listView.setAdapter((ListAdapter) customAdapter);
    }


    public void addValue(View v)
    {
        String name = editTextView.getText().toString();

        if (name.isEmpty())
        {
            Toast.makeText(getApplicationContext(), "Plz enter Values", Toast.LENGTH_SHORT).show();
        }

        else
            {
            Model md = new Model(name);
            ItemModelList.add(md);

            customAdapter.notifyDataSetChanged();
            editTextView.setText("");
        }
    }


}


class CustomAdapter extends BaseAdapter
{
    Context context;
    ArrayList<Model> itemModelList;

    public CustomAdapter(Context context, ArrayList<Model> modelList)
    {
        this.context = context;
        this.itemModelList = modelList;
    }

    @Override
    public int getCount()
    {
        return itemModelList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return itemModelList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        convertView = null;

        if (convertView == null)
        {
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

            convertView = mInflater.inflate(R.layout.row_item01, null);

            TextView tvName = (TextView) convertView.findViewById(R.id.item_name);
            Button btn_delete = (Button) convertView.findViewById(R.id.btn_delete);

            Model m = itemModelList.get(position);
            tvName.setText(m.get());

            btn_delete.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    itemModelList.remove(position);
                    notifyDataSetChanged();
                }
            });
        }
        return convertView;
    }
}



class Model
{
    String name;


    public Model(String name)
    {
        this.name = name;
    }

    public String get()
    {
        return this.name;
    }

    public void set(String  name)
    {
        this.name = name;
    }

}
