/** * 2017-07-31.Book 类 */ public class Book { public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Book(String name, String author, double price) { super(); this.name = name; this.author = author; this.price = price; } private String name; private String author; private double price; }
<!--主xml中,需要用到的ListView--> <ListView android:id="@+id/List" android:layout_width="wrap_content" android:layout_height="wrap_content"></ListView>
<!--itemlist.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="match_parent" android:orientation="horizontal"> <TextView android:id="@+id/Author" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/Name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20pt" /> <TextView android:id="@+id/Price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20pt" /> </LinearLayout>
ArrayAdapter的绑定方法
public class MyMain extends Activity { List<Book> list = new ArrayList<Book>(); ListView listView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //数据写入 GetListValue(); listView = (ListView) findViewById(R.id.List); arrayAdapter dap=new arrayAdapter(this,list); listView.setAdapter(dap); } private void GetListValue() { list.add(new Book("第一行代码 Android 第2版", "郭霖", 57.60)); list.add(new Book("Android从入门到精通", "明日科技", 13.99)); list.add(new Book("疯狂Android讲义(第3版)", "李刚", 75.60)); list.add(new Book("Android开发艺术探索", "任玉刚", 67.20)); } //重新构造ArrayAdapter private class arrayAdapter extends ArrayAdapter<Book> { public arrayAdapter(Context context, List<Book> books) { super(context, 0, 0, books); } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { CheckInfo item; View view = convertView; if (view == null) { view = LayoutInflater.from(MyMain.this).inflate(R.layout.itemlist, parent, false); item = new CheckInfo(view); view.setTag(item); } else { item = (CheckInfo) view.getTag(); } Book book = list.get(position); TextView tName = item.gettName(); TextView tPrice = item.gettPrice(); TextView tAuthor = item.gettAuthor(); tName.setText(book.getName()); tPrice.setText(book.getPrice() + ""); tAuthor.setText(book.getAuthor()); return view; } } private class CheckInfo { TextView tName; TextView tAuthor; TextView tPrice; View view; public CheckInfo(View view) { this.view = view; } public TextView gettName() { if (tName == null) { tName = (TextView) view.findViewById(R.id.Name); } return tName; } public TextView gettAuthor() { if (tAuthor == null) { tAuthor = (TextView) view.findViewById(R.id.Author); } return tAuthor; } public TextView gettPrice() { if (tPrice == null) { tPrice = (TextView) view.findViewById(R.id.Price); } return tPrice; } } }
SimpleAdapter的绑定方法
public class SimpleAdap extends Activity { List<Book> list = new ArrayList<Book>(); ListView view; List<Map<String, String>> maplist = new ArrayList<Map<String, String>>(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GetListValue(); for (Book b : list) { Map<String, String> map = new HashMap<String, String>(); map.put("Author", b.getAuthor()); map.put("Name", b.getName()); map.put("Price", b.getPrice() + ""); maplist.add(map); } view = (ListView) findViewById(R.id.List); SimpleAdapter adapter = new SimpleAdapter( this, maplist, R.layout.itemlist, new String[]{"Author", "Name", "Price"}, new int[]{R.id.Author, R.id.Name, R.id.Price} ); view.setAdapter(adapter); } private void GetListValue() { list.add(new Book("第一行代码 Android 第2版", "郭霖", 57.60)); list.add(new Book("Android从入门到精通", "明日科技", 13.99)); list.add(new Book("疯狂Android讲义(第3版)", "李刚", 75.60)); list.add(new Book("Android开发艺术探索", "任玉刚", 67.20)); } }
BaseAdapter的绑定方法
public class BaseItem extends Activity { List<Book> list=new ArrayList<Book>(); ListView listView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GetListValue(); listView = (ListView) findViewById(R.id.List); Baseadapter b = new Baseadapter(); listView.setAdapter(b); } private void GetListValue() { list.add(new Book("第一行代码 Android 第2版", "郭霖", 57.60)); list.add(new Book("Android从入门到精通", "明日科技", 13.99)); list.add(new Book("疯狂Android讲义(第3版)", "李刚", 75.60)); list.add(new Book("Android开发艺术探索", "任玉刚", 67.20)); } private class Baseadapter extends BaseAdapter { @Override public long getItemId(int position) { return position; } @Override public int getCount() { return list.size(); } @Override public View getView(int position, View convertView, ViewGroup parent) { CheckInfo c; View view = convertView; if (view == null) { view = LayoutInflater.from(BaseItem.this).inflate(R.layout.itemlist, parent, false); c = new CheckInfo(view); view.setTag(c); } else { c = (CheckInfo) view.getTag(); } Book book = list.get(position); TextView name = c.gettName(); TextView price = c.gettPrice(); TextView author = c.gettAuthor(); name.setText(book.getName()); author.setText(book.getAuthor()); price.setText(book.getPrice() + ""); return view; } @Override public Object getItem(int position) { return position; } } private class CheckInfo { TextView tName, tAuthor, tPrice; View view; public CheckInfo(View v) { view = v; } public TextView gettName() { if (tName == null) { tName = (TextView) view.findViewById(R.id.Name); } return tName; } public TextView gettAuthor() { if (tAuthor == null) { tAuthor = (TextView) view.findViewById(R.id.Author); } return tAuthor; } public TextView gettPrice() { if (tPrice == null) { tPrice = (TextView) view.findViewById(R.id.Price); } return tPrice; } } }