zoukankan      html  css  js  c++  java
  • Android学习笔记-listview实现方式之BaseAdapter

    listview是Android开发中最为常用的组件,这里我们就学习一下用BaseAdapter的方式实现listview,

    主布局activity_main.xml是这样的:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/ll_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
        </ListView>
    
    </LinearLayout>
    

    定义一个listitem.xml,这用于在listview组件中进行显示。

    <?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="60dip"
        android:gravity="center_vertical"
        android:orientation="horizontal" >
    
        <TextView
            android:id="@+id/tv_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:text="ID"
            android:textColor="#ff0000"
            android:textSize="18sp" />
    
        <LinearLayout
            android:layout_marginLeft="20dip"
            android:layout_width="fill_parent"
            android:layout_height="60dip"
            android:gravity="center_vertical"
            android:orientation="vertical" >
    
            <TextView
                android:id="@+id/tv_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dip"
                android:text="姓名"
                android:textColor="#000000"
                android:textSize="18sp" />
    
            <TextView
                android:id="@+id/tv_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dip"
                android:text="号码"
                android:textColor="#880000"
                android:textSize="18sp" />
        </LinearLayout>
    
    </LinearLayout>
    

     那么activiy中的实现逻辑是这样的,由于我是在数据库中进行的取值,所以,定义的有dao类,而读者,完全可以自己定义一个arraylist进行数据的展示,

    我们实现的是BaseDdapter,这里呢,这要实现类里面的两个方法,getCount():获取要展示的数据的个数,getView():对数据的展示,这里面有一个重要的方法:inflate(Context context, int resource, ViewGroup root)

      context:是上下文,

      resource:resource The resource ID to inflate,即要展示在listview中的资源文件的id,

      root:root A view group that will be the parent.  Used to properly inflate the * layout_* parameters,这个可以设置位空。

      

    public class MainActivity extends Activity {
    	private LinearLayout ll;
    	private ListView lv;
    	private  List<Person> persons;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            ll = (LinearLayout) findViewById(R.id.ll_root);
            lv = (ListView) findViewById(R.id.lv);
            //获取数据
            PersonDao dao = new PersonDao(this);
            //persons的类型是List<Person>
            persons = dao.findAll();
            
           
            lv.setAdapter(new MyAdapter());
      
        }
        //默认实现类simplexxx,defaultxxx,basexxx.
        //当集成借口比较多的时候,可以继承base,simple,defaule,这些开头
        private class MyAdapter extends BaseAdapter{
    
        	private static final String TAG = "MyAdapter";
    
    		//控制listview里总共有多少条目
    		@Override
    		public int getCount() {
    			// TODO Auto-generated method stub
    			return persons.size();
    		}
    
    		@Override
    		public Object getItem(int position) {
    			// TODO Auto-generated method stub
    			return null;
    		}
    
    		@Override
    		public long getItemId(int position) {
    			// TODO Auto-generated method stub
    			return 0;
    		}
    
    		@Override
    		public View getView(int position, View convertView, ViewGroup parent) {
    		
    			//得到某个位置的person对象。
    			Person person = persons.get(position);
    			//把xml加载进来
    			View view = View.inflate(MainActivity.this, R.layout.listitem, null);
    			
    			//一定要在view对象中寻找孩子的id
    			TextView tv_id = (TextView) view.findViewById(R.id.tv_id);
    			TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
    			TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
    			
    			//person.getId(),是int的,+"" ,int一定要转化成String类型的。
    			//tv_id.setText(person.getId());会出错。
    			tv_id.setText("ID: "+person.getId());
    			tv_name.setText("姓名: "+person.getName());
    			tv_number.setText("电话: "+person.getNumber());
    			
    			return view;
    			
    		}
        	
        }
    
    }
    
  • 相关阅读:
    高数——微分方程的通解和特解
    高数——求极限的方法
    算法训练——删除数组零元素
    算法训练——输出米字形
    算法训练——数的统计
    算法训练——数对
    算法训练——数组查找及替换
    算法训练——数组排序去重
    算法训练——素因子去重
    算法训练——特殊的数字四十
  • 原文地址:https://www.cnblogs.com/fengtengfei/p/3963102.html
Copyright © 2011-2022 走看看