• Android ListView简单实用


    layout创建:

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:orientation="vertical" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        tools:context="com.example.person.MainActivity"
        >
        
        <LinearLayout 
    	    android:layout_width="fill_parent"
    	    android:layout_height="wrap_content"
    	    android:orientation="horizontal" >
    	    <TextView  
    	        android:layout_width="100dp"
    	   		android:layout_height="wrap_content"
    	   		android:text="@string/name"
    	    />
    	    
    	    <TextView  
    	        android:layout_width="100dp"
    	   		android:layout_height="wrap_content"
    	   		android:text="@string/sex"
    	    />
    	    
    	    <TextView  
    	        android:layout_width="100dp"
    	   		android:layout_height="wrap_content"
    	   		android:text="@string/age"
    	    />
    	</LinearLayout>
        
        
        
        <ListView  android:layout_width="fill_parent"
        	android:layout_height="fill_parent"
        	android:id="@+id/listView"
        	/>
        
    </LinearLayout>
    

    item.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >
        
        <TextView  
            android:layout_width="100dp"
       		android:layout_height="wrap_content"
       		android:id="@+id/name"
        />
        
        <TextView  
            android:layout_width="100dp"
       		android:layout_height="wrap_content"
       		android:id="@+id/sex"
        />
        
        <TextView  
            android:layout_width="100dp"
       		android:layout_height="wrap_content"
       		android:id="@+id/age"
        />
    
    </LinearLayout>
    

      

    UserDao  插入数据, 分页查询数据

    /**
     * 
     */
    package com.example.person.dao;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.nfc.Tag;
    import android.os.Build.VERSION;
    import android.util.Log;
    
    /**
     * 
     * @author baoxing.gbx
     * @version $Id: DBopenHelper.java, v 0.1 2015年7月11日 下午9:39:03 baoxing.gbx Exp $
     */
    public class DBopenHelper extends SQLiteOpenHelper{
    
        /** 数据库名称  */
        private static final String DB_NAME = "test.db";
        
        private static final int  VERSION = 1;
        
        private static final String Tag = "DBopenHelper";
        
       
        // 初始化数据库开启装置
        public DBopenHelper(Context context) {
            super(context, DB_NAME, null, VERSION);
            // TODO Auto-generated constructor stub
        }
    
        // 
        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.i(Tag, "onCreate 被调用");
            db.execSQL("create table user (id integer primary key autoincrement, name varchar(20),sex varchar(20), age integer )");
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.i(Tag, "onUpgrade 被调用");
            db.execSQL("DROP TABLE IF EXISTS person");
            onCreate(db);
        }
    
    }
    
    /**
     * 
     */
    package com.example.person.dao;
    
    import java.util.ArrayList;
    import java.util.List;
    import android.annotation.SuppressLint;
    import android.content.ContentValues;
    import android.database.Cursor;
    import com.example.person.vo.User;
    
    /**
     * 
     * @author baoxing.gbx
     * @version $Id: UserDao.java, v 0.1 2015年7月11日 下午10:44:09 baoxing.gbx Exp $
     */
    public class UserDao {
        String tableName = "user";
        DBopenHelper dbopenHelper;
        
        public UserDao (DBopenHelper dBopenHelper) {
            this.dbopenHelper = dBopenHelper;
        }
        
        public long save(User user) {
            ContentValues values = new ContentValues();
            values.put("name", user.getName());
            values.put("sex", user.getSex());
            values.put("age", user.getAge());
            long result = dbopenHelper.getWritableDatabase().insert(tableName, null, values);
            return result;
        }
        
        public List<User> queryPage(int startRow, int pageSize) {
            String limit = startRow + "," + pageSize;
            Cursor cursor = dbopenHelper.getReadableDatabase().query(tableName, null, null, null, null, null, null, limit);
            List<User> users = new ArrayList<User>();
            while (cursor.moveToNext()) {
                User user = new User();
                user.setId(cursor.getInt(cursor.getColumnIndex("id")));
                user.setName(cursor.getString(cursor.getColumnIndex("name")));
                user.setSex(cursor.getString(cursor.getColumnIndex("sex")));
                user.setAge( cursor.getInt(cursor.getColumnIndex("age")));
                users.add(user);
            }
            return users;
        }
    
    }
    

      

    MainActivity显示数据

    package com.example.person;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.SimpleCursorAdapter;
    
    import com.example.person.dao.DBopenHelper;
    import com.example.person.dao.UserDao;
    import com.example.person.vo.User;
    
    public class MainActivity extends ActionBarActivity {
        
        private ListView listView;
        
        private UserDao userDao ;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listView = (ListView)findViewById(R.id.listView);
            userDao = new UserDao(new DBopenHelper(this));
            
            // 简单适配器
    //        show1();
            
            // 简单适配器
            show2();
            
        }
        
        /**
         * 自定义是配置显示
         */
        private void show2() {
            List<User> users = userDao.queryPage(0, 20);
            
            listView.setAdapter(new MyAdapter(this, users, R.layout.item));
        }
    
        /**
         * 普通是配置适配ListView
         */
        private void show1() {
            List<User> users = userDao.queryPage(0, 20);
            List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
            for (User user : users) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("name", user.getName());
                map.put("sex", user.getSex());
                map.put("age", user.getAge());
                data.add(map);
            }
            SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, 
                new String[] {"name", "sex", "age"}, new int[] {R.id.name, R.id.sex, R.id.age});
            
            listView.setAdapter(adapter);
        }
    }
    

      

    自定义适配器

    package com.example.person;
    
    import java.util.List;
    
    import com.example.person.vo.User;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    
    public class MyAdapter extends BaseAdapter{
        
        private List<User> users;
        private int  resource;
        
        private LayoutInflater inflater; 
    
        public MyAdapter(Context context,List<User> users, int resource) {
            super();
            this.users = users;
            this.resource = resource;
            this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    
        public MyAdapter() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        @Override
        public int getCount() {
            return users.size();
        }
    
        @Override
        public Object getItem(int position) {
            return users.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (null == convertView) {
                convertView = inflater.inflate(resource, null);
            }
           ( (TextView)convertView.findViewById(R.id.name)).setText(users.get(position).getName());
           ( (TextView)convertView.findViewById(R.id.sex)).setText(users.get(position).getSex());
           ( (TextView)convertView.findViewById(R.id.age)).setText(users.get(position).getAge()+"");
            return convertView;
        }
    
    }
    

      

  • 相关阅读:
    Spring 循环依赖的三种方式(三级缓存解决Set循环依赖问题)
    终于有人把“TCC分布式事务”实现原理讲明白了
    Java synchronized 关键字的实现原理
    Synchronized的实现原理(汇总)
    Spring的Bean的生命周期(大众版)
    Synchronized与Lock的区别与应用场景
    Lock与synchronized 的区别
    线程的同步控制synchronized和lock的对比和区别
    lock和synchronized的同步区别与选择
    Mybatis3.x与Spring4.x整合
  • 原文地址:https://www.cnblogs.com/E-star/p/4678082.html
Copyright © 2020-2023  润新知