1. 程式人生 > >安卓ListView自定義Adapter

安卓ListView自定義Adapter

list.xml

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

item.xml

    <TextView
        android:id="@+id/name"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        app:layout_constraintRight_toLeftOf="@+id/price"/>
    <TextView
        android:id="@+id/price"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        app:layout_constraintLeft_toRightOf="@+id/name"/>

Book.java

public class Book {
    private String bookName;
    private String bookPrice;
    public Book(String bookName, String bookPrice) {
        this.bookName = bookName;
        this.bookPrice = bookPrice;
    }
    public String getBookName() {
        return bookName;
    }

    public String getBookPrice() {
        return bookPrice;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public void setBookPrice(String bookPrice) {
        this.bookPrice = bookPrice;
    }
}

list.java

        List<Book> data = new ArrayList<>();
        for(int i = 0; i < 3; i++) {
            Book b = new Book("book","100");
            data.add(b);
        }

        ListView listview = (ListView) findViewById(R.id.listview);
        MyListViewAdapter t = new MyListViewAdapter(this, data);
        listview.setAdapter(t);
MyListViewAdapter.java
public class MyListViewAdapter extends BaseAdapter {
    private List<Book> list;
    LayoutInflater inflater;
    public MyListViewAdapter(Context context, List<Book> list) {
        this.list = list;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Object getItem(int i) {
        if (list == null) {
            return null;
        }
        return list.get(i);
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        // 當view為空時才載入佈局,否則,直接修改內容
        if (convertView == null) {
            // 通過inflate的方法載入佈局,context需要在使用這個Adapter的Activity中傳入。
            convertView = inflater.inflate(R.layout.item, null);
            viewHolder = new ViewHolder();
            viewHolder.bookName = (TextView) convertView.findViewById(R.id.name);
            viewHolder.bookPrice = (TextView) convertView.findViewById(R.id.price);
            convertView.setTag(viewHolder); // 用setTag方法將處理好的viewHolder放入view中
        } else { // 否則,讓convertView等於view,然後從中取出ViewHolder即可
            viewHolder = (ViewHolder) convertView.getTag();
        }
        // 從viewHolder中取出對應的物件,然後賦值給他們
        viewHolder.bookName.setText(list.get(i).getBookName());
        viewHolder.bookPrice.setText(list.get(i).getBookPrice());
        // 將這個處理好的view返回
        return convertView;
    }
    private class ViewHolder {
        public TextView bookName;
        public TextView bookPrice;
    }
}