常用数据适配器
在使用ListView控件时,需要进行数据适配,这样界面才会显示出数据。在进行数据适配时会用到数据适配器,所谓的数据适配器就是数据与视图之间的桥梁,他就类似于一个转换器,将复杂的数据转换器切换成用户可以接受的方式来呈现。
-
使用数据适配器的基本流程
- 1、finds the data source
- 2、gets the item count
- 3、gets the item layout
- 4、renders(渲染) the item view
-
BaseAdapter
- BaseAdapter是最基本的适配器。他实际上就是一个抽象类,通常在使用自定义适配器时需要继承BaseAdapter,该类拥有4个抽象方法。
方法名称 功能描述 public int getCunt() 得到Item条目的总数 public Object getItem(int position) 根据position(位置)得到某个Item的对象 public long getItemId(int positionn) 根据position(位置)得到某个Item的id public View getView(int position,View convertView,ViewGroup parent) 得到相应position对应的Item的视图,position是当前的Item的位置,convertView用于复用旧视图,parent用于加载XML布局 -
水果商店的小布局:
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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/listView_fruit" android:layout_width="367dp" android:layout_height="395dp" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button" app:layout_constraintVertical_bias="0.794" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:layout_marginBottom="8dp" android:text="欢迎选购" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.108" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.063" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:layout_marginBottom="8dp" android:text="显示水果" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.233" app:layout_constraintStart_toEndOf="@+id/textView" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.042" /> </android.support.constraint.ConstraintLayout>
- fruits_item.xml
<?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="100dp" android:orientation="horizontal"> <ImageView android:id="@+id/imageView" android:background="@mipmap/ic_launcher" android:layout_width="80dp" android:layout_height="80dp" /> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView_name" android:text="名称" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/textView_price" android:text= "价格" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
- MainActivity.java
package com.example.fruitsshop; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { //数据源 private String[] names = {"苹果","樱桃","草莓","柠檬","龙眼"}; private double[] price = {1.1,2.2,3.3,4.3,5.5}; private int[] images = {R.drawable.apple,R.drawable.yingtao,R.drawable.strawberry,R.drawable.lemon,R.drawable.longyan}; private Button btn1; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = findViewById(R.id.button); listView = findViewById(R.id.listView_fruit); //数据源填充listView FruitAdapter adapter = new FruitAdapter(); listView.setAdapter(adapter); //设置监听 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Log.i("Fruits","你点击了" + names[position]); Toast toast = Toast.makeText(MainActivity.this, "你点击了" + names[position], Toast.LENGTH_SHORT); toast.show(); } }); } //适配器类,用于完成水果数据到listView的填充工作 public class FruitAdapter extends BaseAdapter{ //重点 @Override public int getCount() { return names.length; } @Override public Object getItem(int position) { return names[position]; } @Override public long getItemId(int position) { return position; } //重点:逐行填充水果数据项内容 @Override public View getView(int position, View convertView, ViewGroup parent) { //加载单项数据布局 View itemView = View.inflate(MainActivity.this,R.layout.fruits_item,null); //逐个的填写图片、名称和价格 ImageView image = itemView.findViewById(R.id.imageView); image.setBackgroundResource(images[position]); TextView tv_name = itemView.findViewById(R.id.textView_name); tv_name.setText(names[position]); TextView tv_price = itemView.findViewById(R.id.textView_price); tv_price.setText(price[position] + "RMB/500g"); return itemView; } } }
- activity_main.xml