package jp.gihyo.mitoma.android.p031memberlist;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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

        ListView listView = findViewById(R.id.lvMember);
        MemberDS memberDS = MemberDS.getInstance();
        ArrayList<MemberDTO> memberList = memberDS.select();

        //カスタムアダプタセット
        CustomAdapter adapter = new CustomAdapter(
                this, R.layout.item_layout,memberList);
        listView.setAdapter(adapter);
        //リスナーセット
        listView.setOnItemClickListener(new MyListener());
    }
    private class MyListener implements AdapterView.OnItemClickListener {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            MemberDTO dto = (MemberDTO) parent.getItemAtPosition(position);
            ListView listView = findViewById(R.id.lvMember);
            boolean checked = listView.isItemChecked(position);

            if (checked) {
                Toast.makeText(MainActivity.this, dto.memberName + "さんが選択されました", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(MainActivity.this, dto.memberName + "さんが選択解除されました", Toast.LENGTH_LONG).show();
            }
        }
    }
}