package cn.edu.xjau.customlistviewadapterexam;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ListViewCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

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

/**
 * Created by Administrator on 2018/3/7 0007.
 */

public class MainActivity extends Activity {
    private ListView lvProduct;
    private ProductListAdapter adapter;
    private List<Product> mProductList;

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

        lvProduct = (ListView) findViewById(R.id.listview_product);

        mProductList = new ArrayList<>();

        mProductList.add(new Product(1, "iphone4", 200, "apple iphone4 16GB"));
        mProductList.add(new Product(2, "iphone4s", 250, "apple iphone4s 16GB"));
        mProductList.add(new Product(3, "iphone5", 300, "apple iphone5 16GB"));
        mProductList.add(new Product(4, "iphone5s", 350, "apple iphone5s 16GB"));
        mProductList.add(new Product(5, "iphone6", 400, "apple iphone6 16GB"));
        mProductList.add(new Product(6, "iphone6s", 450, "apple iphone6s 16GB"));
        mProductList.add(new Product(7, "iphone7", 500, "apple iphone7 16GB"));
        mProductList.add(new Product(8, "iphone7s", 550, "apple iphone7s 16GB"));
        mProductList.add(new Product(9, "iphone8", 600, "apple iphone8 16GB"));
        mProductList.add(new Product(10, "iphone8s" , 650, "apple iphone8s 16GB"));

        adapter = new ProductListAdapter(getApplicationContext() , mProductList);
        lvProduct.setAdapter(adapter);

        lvProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext() , "clicked product id = " + view.getTag(), Toast.LENGTH_SHORT).show();
            }
        });

    }
}
