安卓:自定义适配器
activity_main
<?xml version="1.0" encoding="utf-8"?>
<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 小项
<?xml version="1.0" encoding="utf-8"?>
<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) {
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;
}
}
实现效果图
|