package com.example.jyandroid.listview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import com.example.jyandroid.R;
import java.util.ArrayList;
import java.util.List;
public class ListView2Activity extends AppCompatActivity {
// 声明组件
private ListView listview2;
private List<Product> productList = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view2);
// 获取组件对象
listview2 = findViewById(R.id.listview2);
// 模拟数据
String[] datas = {"商品1","商品2","商品3","商品4"};
Double[] prices = {11.11,22.22,33.33,44.44};
int[] imgIds = {R.mipmap.dou,R.mipmap.heihei,R.mipmap.huahua,R.mipmap.tiantian};
// 将数据封装至模板
productList = new ArrayList<>();
for (int i = 0; i < datas.length; i++) {
Product product = new Product();
product.setTitle(datas[i]);
product.setPrice(prices[i]);
product.setImgId(imgIds[i]);
productList.add(product);
}
// 利用适配器进行组装
MyAdapter myAdapter = new MyAdapter(productList,this);
// 接通视图
listview2.setAdapter(myAdapter);
}
}