package com.example.patrice.http01;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;

// http://www.worldbestlearningcenter.com/tips/Android-ListView-item-and-button-clickable.htm

// http://www.androidinterview.com/android-custom-listview-with-checkbox-example/


public class ListViewItem01 extends AppCompatActivity
{
    Context context;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listviewitem01_layout);

        context=this;

        ListView lstview=(ListView)findViewById(R.id.listv);

        lstview.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                Toast.makeText(context, "An item of the ListView is clicked.", Toast.LENGTH_LONG).show();
            }
        });

        String[] items={"1","2","3","4","5"};
        LstViewAdapter adapter=new LstViewAdapter(this,R.layout.row_item_layout,R.id.txt,items);
        // Bind data to the ListView
        lstview.setAdapter(adapter);
    }

    public void clickMe(View view)
    {
        Button bt=(Button)view;
        Toast.makeText(this, "Button "+bt.getText().toString(),Toast.LENGTH_LONG).show();
    }

}


class LstViewAdapter extends ArrayAdapter<String>
{
    int groupid;
    String[] item_list;
    ArrayList<String> desc;
    Context context;

    public LstViewAdapter(Context context, int vg, int id, String[] item_list)
    {
        super(context,vg, id, item_list);
        this.context=context;
        groupid=vg;
        this.item_list=item_list;

    }
    // Hold views of the ListView to improve its scrolling performance
    static class ViewHolder
    {
        public TextView textview;
        public Button button;

    }

    public View getView(int position, View convertView, ViewGroup parent)
    {

        View rowView = convertView;
        // Inflate the list_item.xml file if convertView is null
        if(rowView==null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView= inflater.inflate(groupid, parent, false);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.textview= (TextView) rowView.findViewById(R.id.txt);
            viewHolder.button= (Button) rowView.findViewById(R.id.bt);
            rowView.setTag(viewHolder);

        }

        // Set text to each TextView of ListView item
        ViewHolder holder = (ViewHolder) rowView.getTag();
        holder.textview.setText(item_list[position]);
        holder.button.setText(item_list[position]);
        return rowView;
    }

}
