基本内容
- 如果有列表视图,就要用适配器(要记住)
用实训案例来了解
1.创建一个安卓项目
2.主布局资源文件activity_main.xml
- 列表控件,
<ListView>
- 以纵向列表的方式显示文本内容,实现纵向滚动效果
- 单击列表视图中选中的选项,弹出Toast提示
<?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>
3.准备好图片资源
4.建立一个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>
5.类文件MainActivity.java 代码
package net.yuanjing.adapter;
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);
listViewProduct.setAdapter(productAdapter);
listViewProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Product product = (Product) parent.getItemAtPosition(position);
Toast.makeText(MainActivity.this,"点击了" + product.getName()+";库存还有:" + product.getStocks(),Toast.LENGTH_LONG).show();
}
});
}
}
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) ;
}
}
6.建立一个Product
package net.yuanjing.adapter;
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 +
'}';
}
}
7.建立一个ProductAdapter文件
package net.yuanjing.adapter;
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) {
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;
}
}
8.运行一下,看哈效果