zoukankan      html  css  js  c++  java
  • Android ArrayAdapter,BaseAdapter,SimpleAdapter适配器绑定数据方法

    /**
     * 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;
            }
        }
    }
    View Code

     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));
        }
    }
    View Code

    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;
            }
        }
    }
    View Code
  • 相关阅读:
    nuget 微软开源项目工具库
    Creating a REST service using ASP.NET Web API
    [转载]返璞归真 asp.net mvc (10) asp.net mvc 4.0 新特性之 Web API
    【转载】返璞归真 asp.net mvc (11) asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, .net 4.5 带来的更方便的异步操作
    ASP.NET MVC3使用PagedList实现分页导航
    asp.net mvc api 异常捕捉
    atitit.提升软件开发效率大的总结O5
    atitit.dw不能显示正确的百分比高度in dw的解决
    atitit.常用编程语言的性能比较 c c++ java
    atitit.二维码生成总结java zxing
  • 原文地址:https://www.cnblogs.com/imluzhi/p/7268028.html
Copyright © 2011-2022 走看看