zoukankan      html  css  js  c++  java
  • ListView和BaseAdapter

     参考资料:http://www.runoob.com/w3cnote/android-tutorial-listview.html

    <LinearLayout 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"
        android:orientation="vertical"
        tools:context="com.expample.myapplication.MainActivity">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/mainView_id"/>
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:id="@+id/subView_id"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="5dp"
            android:background="#000000"/>
    
    </LinearLayout>
    public class MainActivity extends Activity {
        ListView lv;
        List ls;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            lv = (ListView)findViewById(R.id.listView_id);
            ls = new LinkedList<animals>();
            ls.add(new animals("猪说", "我是猪吗?"));
            ls.add(new animals("猫说", "我是猫吗?"));
            ls.add(new animals("狗说", "我是狗吗?"));
            ls.add(new animals("熊说", "我是熊吗?"));
            ls.add(new animals("兔说", "我是兔吗?"));
            ls.add(new animals("虎说", "我是虎吗?"));
            ls.add(new animals("鼠说", "我是鼠吗?"));
            ls.add(new animals("鸡说", "我是鸡吗?"));
            ls.add(new animals("鱼说", "我是鱼吗?"));
            MyAdapter adapter = new MyAdapter((LinkedList<animals>)ls, MainActivity.this);
    
            lv.setAdapter(adapter);
        }
    
        class animals{
            String main;
            String sub;
    
            animals(String main, String sub){
                this.main = main;
                this.sub = sub;
            }
    
            String getMain(){
                return main;
            }
    
            String getSub(){
                return sub;
            }
        }
        class MyAdapter extends BaseAdapter{
            LinkedList<animals> mdata;
            Context mcontext;
            MyAdapter(LinkedList<animals> data, Context context){
                this.mdata = data;
                this.mcontext = context;
            }
            @Override
            public int getCount() {
                return mdata.size();
            }
    
            @Override
            public Object getItem(int position) {
                return mdata.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView main;
                TextView sub;
    
                LayoutInflater layoutInflater = LayoutInflater.from(mcontext);
                convertView = layoutInflater.inflate(R.layout.item_list_animal, null, false);
                main = (TextView) convertView.findViewById(R.id.mainView_id);
                sub = (TextView)convertView.findViewById(R.id.subView_id);
    
                main.setText(mdata.get(position).getMain());
                sub.setText(mdata.get(position).getSub());
                return convertView;
            }
        }
    }

  • 相关阅读:
    Python【第五篇】模块、包、常用模块
    Python【第四篇】函数、内置函数、递归、装饰器、生成器和迭代器
    TCP三次握手、四次挥手
    分别用postman和python做post请求接口功能测试
    Python【第三篇】文件操作、字符编码
    Python【第二篇】运算符及优先级、数据类型及常用操作、深浅拷贝
    Python【第一篇】python安装、pip基本用法、变量、输入输出、流程控制、循环
    oracle在windows(含客户端工具pl/sql安装)下安装
    Python【初识篇】简介
    Web jsp开发自学——ajax+servlet+echarts+json+gson 实现ajax传输servlert和echarts的数据,可视化结果
  • 原文地址:https://www.cnblogs.com/maogefff/p/8168124.html
Copyright © 2011-2022 走看看