ListView與自定義介面卡(顯示java端的資料)
阿新 • • 發佈:2018-12-10
一、自定義介面卡 1、BaseAdapter:是所有介面卡類的父類,可以對列表項進行最大限度的定製 2、自定義介面卡中的方法
@Override
public int getCount() {//從java端獲取到多少條資料
return data.size();
}
@Override
public Object getItem(int i) {//獲取到具體的資料
return data.get(i);
}
@Override
public long getItemId(int i) {//獲取到第幾條資料
return i;
}
@Override
public View getView (int i, View view, ViewGroup viewGroup) {
return null;
}
2、LayoutInflater(佈局解析器) LayoutInflater有三種獲得方式,資料中有詳細介紹 用來把layout佈局檔案解析成一個View物件,不可以new,需要使用系統服務獲得
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
3、案例:展示資料(書本打比方) ①、book.java(實體類)
package com.example.t212_09;
public class Book {
private Integer id;
private String title;
private String author;
private Float price;
private String publish;
private String remark;
private int image;
public Book() {
super();
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
//資料庫中也需要這幾個列
②、展示所有的dao方法
package com.example.t212_09;
import java.util.ArrayList;
import java.util.List;
public class BookDao {
private int[] bookImages = new int[] { R.drawable.book1, R.drawable.book2,
R.drawable.book3, R.drawable.book4, R.drawable.book5,
R.drawable.book6, R.drawable.book7, R.drawable.book8,
R.drawable.book9, R.drawable.book10 };
public List<Book> list() {
List<Book> list = new ArrayList<Book>();
for (int i = 1; i <= 15; i++) {
Book b = new Book();
b.setId(i);
b.setTitle("t" + i);
b.setAuthor("a" + i);
b.setPrice(0.0f + i);
b.setPublish("p" + i);
b.setRemark("r" + i);
//真實開發圖片應該是從網路獲取,而非本地獲得
b.setImage(bookImages[i % bookImages.length]);
list.add(b);
}
return list;
}
}
③、重新建立一個佈局xml(展示資料的佈局)
listview_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="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_listviewitem_image"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:padding="10dp"
android:scaleType="fitXY"
android:src="@drawable/book1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/tv_listviewitme_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="狂人攝影日記"
android:textColor="@color/red"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="書本作者:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_listviewitme_author"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="阿劉" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="書本價格:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_listviewitme_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="$123"
android:textColor="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="出版社:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_listviewitme_publish"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="電子出版社"
android:textColor="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="書本簡介:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_listviewitme_remark"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ellipsize="end"
android:maxLines="2"
android:text="很冷的一個冬天很冷的一個冬天很冷的一個冬天很冷的一個冬天很冷的一個冬天很冷的一個冬天很冷的一個冬天很冷的一個冬天很冷的一個冬天很冷的一個冬天"
android:textColor="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/bt_listviewitme_btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:src="@drawable/btn_shopping" />
<ImageButton
android:id="@+id/bt_listviewitme_btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:src="@drawable/btn_accounts" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
④、在activity_main_xml中需要設定一個id
<?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/lv_main_bookList"//設定的id
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
⑤、MainActivity.java(在這裡寫方法)
package com.example.t212_09;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Book> data;
private MyBaseAdapter adapter;
private ListView lv_main_bookList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_main_bookList = findViewById(R.id.lv_main_bookList);
this.data = new BookDao().list();
adapter = new MyBaseAdapter((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE));
lv_main_bookList.setAdapter(adapter);//繫結
}
public class MyBaseAdapter extends BaseAdapter{//內部類
public class ViewHolder{
ImageView iv_listviewitem_image;
TextView tv_listviewitme_title;
TextView tv_listviewitme_author;
TextView tv_listviewitme_price;
TextView tv_listviewitme_publish;
TextView tv_listviewitme_remark;
}
private LayoutInflater inflater;
public MyBaseAdapter(LayoutInflater inflater) {
this.inflater = inflater;
}
@Override
public int getCount() {//從java端獲取到多少條資料
return data.size();
}
@Override
public Object getItem(int i) {//獲取到具體的資料
return data.get(i);
}
@Override
public long getItemId(int i) {//獲取到第幾條資料
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
//獲取資源,把項資源解析成可見的View檢視
// View v = inflater.inflate(R.layout.listview_item,null);
//減少解析項資源的時間(優化)
View v = view;
if(v == null){
ViewHolder vh = new ViewHolder();//優化
Log.i("test","position"+i);
v = inflater.inflate(R.layout.listview_item,null);
// 找到所有的展示內容 獲取項資源裡的所有控制元件
vh.iv_listviewitem_image = v.findViewById(R.id.iv_listviewitem_image);
vh.tv_listviewitme_title = v.findViewById(R.id.tv_listviewitme_title);
vh.tv_listviewitme_author = v.findViewById(R.id.tv_listviewitme_author);
vh.tv_listviewitme_price = v.findViewById(R.id.tv_listviewitme_price);
vh.tv_listviewitme_publish = v.findViewById(R.id.tv_listviewitme_publish);
vh.tv_listviewitme_remark = v.findViewById(R.id.tv_listviewitme_remark);
//儲存控制元件
v.setTag(vh);
}
//有多少條資料執行多少遍(用來傳值的)
ViewHolder vh = (ViewHolder) v.getTag();
//填充內容
Book book = data.get(i);
vh.iv_listviewitem_image.setImageResource(book.getImage());//圖片
vh.tv_listviewitme_title.setText(book.getTitle());//書名
vh.tv_listviewitme_author.setText(book.getAuthor());//作者
vh.tv_listviewitme_price.setText(book.getPrice()+"");//價格
vh.tv_listviewitme_publish.setText(book.getPublish());//出版社
vh.tv_listviewitme_remark.setText(book.getRemark());//書本簡介
return v;
}
}
}
最後展示的結果: