安卓:自定义适配器

安卓:自定义适配器

activity_main


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/lv_product"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

LinearLayout>

item 小项


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <ImageView
        android:id="@+id/product_picture"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/caomei"
        />
    <LinearLayout
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/product_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:textSize="25dp"
            android:text="丹东草莓"/>


        <LinearLayout
            android:layout_marginTop="15dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/product_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="25dp"
                android:textColor="#ff2121"
                android:text="¥20"/>
            <TextView
                android:layout_marginStart="70dp"
                android:id="@+id/product_stocks"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:textSize="25dp"
                android:text="库存量"/>
        LinearLayout>
    LinearLayout>
LinearLayout>

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<Product> 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<Product> makeData() {
        List<Product> 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<Product> listProduct ;
    //子控件
    private int layout ;
    //构造方法传入数据

    public ProductAdapter(Context mcontext, List<Product> 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;
    }
}

实现效果图

安卓:自定义适配器_第1张图片

你可能感兴趣的:(安卓开发,android,android,studio,java)