package jp.gihyo.mitoma.android.p013gdplist;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
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.lvCountry);
        // Adapter の生成
        CountryDAO friendsDS = CountryDAO.getInstance();
        ArrayList<CountryDTO> countryList = friendsDS.select();
        ArrayAdapter<CountryDTO> adapter = new ArrayAdapter<>(
                this,R.layout.custom_list_item,countryList);
        // AdapterをListViewにセット
        listView.setAdapter(adapter);
        // アイテムクリックリスナーをセット
        listView.setOnItemClickListener(new MyClickListener());
    }
    private class MyClickListener implements AdapterView.OnItemClickListener{
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // クリック行に対応するDTOを取得
            CountryDTO dto = (CountryDTO)parent.getItemAtPosition(position);
            // DTOの表示
            EditText etCno = findViewById(R.id.etCno);
            etCno.setText(String.valueOf(dto.cNo));
            EditText etCountryName = findViewById(R.id.etCountryName);
            etCountryName.setText(dto.countryName);
            EditText etGDP = findViewById(R.id.etGDP);
            etGDP.setText(String.valueOf(dto.gdp));
            EditText etPop = findViewById(R.id.etPopulation);
            etPop.setText(String.valueOf(dto.population));

        }
    }
}