安卓开发|安卓(自定义适配器)

安卓:自定义适配器 activity_main


item 小项

MainActivity
package com.example.demoadapter; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { //控件 private ListView listViewProduct ; //数据 List listProduct ; //自定义适配器 ProductAdapter productAdapter ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取控件 getView() ; //准备数据 listProduct = makeData(); //实例化适配器 productAdapter = new ProductAdapter(this , listProduct,R.layout.item ); //绑定适配器 listViewProduct.setAdapter(productAdapter); }private List makeData() { List productList = new ArrayList<>() ; Product caomei = new Product(R.mipmap.caomei , "草莓","21.5",11 ) ; productList.add(caomei) ; Product dagao = new Product(R.mipmap.dagao , "打糕" ,"9.5",15 ) ; productList.add(dagao) ; Product xiaobei = new Product(R.mipmap.xiaobei , "肉松小贝" ,"25" ,61 ) ; productList.add(xiaobei) ; Product zhishi = new Product(R.mipmap.zhishi , "芝士饼干" , "19" , 12 ) ; productList.add(zhishi) ; return productList ; }private void getView() { listViewProduct = findViewById(R.id.lv_product) ; } } ···## Product ```java package com.example.demoadapter; public class Product { private int picture; //图片 private String name ; //名字 private String price ; //价钱 private int stocks ; //库存量public Product(int picture, String name, String price, int stocks) { this.picture = picture; this.name = name; this.price = price; this.stocks = stocks; }public int getPicture() { return picture; }public void setPicture(int picture) { this.picture = picture; }public String getName() { return name; }public void setName(String name) { this.name = name; }public String getPrice() { return price; }public void setPrice(String price) { this.price = price; }public int getStocks() { return stocks; }public void setStocks(int stocks) { this.stocks = stocks; }@Override public String toString() { return "Product{" + "picture=" + picture + ", name='" + name + '\'' + ", price='" + price + '\'' + ", stocks=" + stocks + '}'; } }

ProductAdapter
package com.example.demoadapter; import android.content.Context; import android.content.Intent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.List; public class ProductAdapter extends BaseAdapter { //上下文环境 private Context mcontext ; //数据源 private List listProduct ; //子控件 private int layout ; //构造方法传入数据public ProductAdapter(Context mcontext, List listProduct, int layout) { this.mcontext = mcontext; this.listProduct = listProduct; this.layout = layout; }@Override public int getCount() { return listProduct.size(); }@Override public Object getItem(int i) { return listProduct.get(i); }@Override public long getItemId(int i) { return i; }@Override public View getView(int i, View view, ViewGroup viewGroup) { if(null == view) { //获取item控件 LayoutInflater inflater = LayoutInflater.from(mcontext) ; view = inflater.inflate(R.layout.item , null) ; } //获取子布局中控件 ImageView picture = view.findViewById(R.id.product_picture) ; TextView name = view.findViewById(R.id.product_name) ; TextView price = view.findViewById(R.id.product_price) ; TextView stocks = view.findViewById(R.id.product_stocks) ; //获取当前的元素 Product product = listProduct.get(i) ; //绑定数据 picture.setImageResource(product.getPicture()); name.setText(product.getName()); price.setText("¥" + product.getPrice()); stocks.setText("库存量" + product.getStocks()); return view; } }

实现效果图 【安卓开发|安卓(自定义适配器)】安卓开发|安卓(自定义适配器)
文章图片

    推荐阅读