android_20_ListView_BaseAdapter
来源:互联网 发布:淘宝手办哪家好 编辑:程序博客网 时间:2024/06/10 13:01
效果:
清单文件:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sg31.listviewwithbaseadapter" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> // 测试用的,注意包名和节点位置 <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.sg31.listviewwithbaseadapter"></instrumentation> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <uses-library android:name="android.test.runner"/> <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.sg31.listviewwithbaseadapter.MainActivity" > <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" ></ListView></LinearLayout>
自定义cell:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="名字" android:textSize="25sp" /><LinearLayout android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/tv_pwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码" /> <TextView android:id="@+id/tv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="手机" /></LinearLayout></RelativeLayout>
模型类:
package com.sg31.listviewwithbaseadapter;public class User {public String _id;public String name;public String pwd;public String phone;public User(String _id2, String name2, String pwd2, String phone2) {this._id = _id2;this.name = name2;this.pwd = pwd2;this.phone = phone2;}public String get_id() {return _id;}public void set_id(String _id) {this._id = _id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}@Overridepublic String toString() {return this._id + ";" + this.name + ";" + this.pwd + ";" + this.phone;}}
数据库帮助类:
package com.sg31.listviewwithbaseadapter;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.database.sqlite.SQLiteDatabase.CursorFactory;public class SGDBHelper extends SQLiteOpenHelper {public SGDBHelper(Context context, String name, CursorFactory factory,int version) {super(context, name, factory, version);}@Overridepublic void onCreate(SQLiteDatabase db) {// 数据库创建时,建表System.out.println("数据库创建时调用");db.execSQL("create table user(_id integer primary key autoincrement, name char(10), pwd char(10), phone integer(20))");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {System.out.println("数据库升级时调用");}}
单元测试类:
package com.sg31.listviewwithbaseadapter;import android.content.ContentValues;import android.database.sqlite.SQLiteDatabase;import android.test.AndroidTestCase;public class SQLDatabaseTest extends AndroidTestCase {private SGDBHelper dbHelper;private SQLiteDatabase db;@Overrideprotected void setUp() throws Exception {super.setUp();// 初始化// 建立数据库引用dbHelper = new SGDBHelper(getContext(), "user.sqlite", null, 1);db = dbHelper.getWritableDatabase();System.out.println("sg__"+db.toString());}@Overrideprotected void tearDown() throws Exception {super.tearDown();// 收尾工作db.close();}// 使用封装好的API进行CRUDpublic void testInsertApi(){System.out.println("testInsertApi");for (int i = 0; i < 100; i++) {//把要插入的数据全部封装至ContentValues对象ContentValues values = new ContentValues();values.put("name", "shuiying"+i);values.put("pwd", "sg31.com");values.put("phone", 1346943+i);long _id = db.insert("user", null, values);System.out.println("sg__"+_id);} }}
控制器:
package com.sg31.listviewwithbaseadapter;import java.util.ArrayList;import java.util.List;import android.support.v7.app.ActionBarActivity;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends ActionBarActivity {List<User> userList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); loadDataFromDB(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } // 从数据库中查询,并显示到界面 private void loadDataFromDB() { userList = new ArrayList<User>(); //把数据库的数据查询出来 SGDBHelper oh = new SGDBHelper(this, "user.sqlite", null, 1); SQLiteDatabase db = oh.getWritableDatabase(); Cursor cursor = db.query("User", null, null, null, null, null, null, null); while(cursor.moveToNext()){ String _id = cursor.getString(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); String pwd = cursor.getString(cursor.getColumnIndex("pwd")); String phone = cursor.getString(cursor.getColumnIndex("phone")); User p = new User(_id, name,pwd, phone); userList.add(p); } // 使用ListView进行展示 ListView listView = (ListView) findViewById(R.id.listView); listView.setAdapter(new SGBaseAdapter()); } class SGBaseAdapter extends BaseAdapter{ //系统调用,用来获知集合中有多少条元素 @Override public int getCount() { return userList.size(); } //由系统调用,获取一个View对象,作为ListView的条目 //position:本次getView方法调用所返回的View对象,在listView中是处于第几个条目,那么position的值就是多少 @Override public View getView(int position, View convertView, ViewGroup parent) { User p = userList.get(position); // 先从缓存池中取,取不到,new一个 View v = convertView; CellViewHolder cellViewHolder = null; if (v != null) { cellViewHolder = (CellViewHolder)v.getTag();} //判断条目是否有缓存 if(v == null){ //把布局文件填充成一个View对象 v = View.inflate(MainActivity.this, R.layout.list_cell, null); // cell内部的控件也要缓存,通过资源id查找组件,注意调用的是View对象的findViewById cellViewHolder = new CellViewHolder(); cellViewHolder.tv_Name = (TextView)v.findViewById(R.id.tv_name); cellViewHolder.tv_Pwd = (TextView)v.findViewById(R.id.tv_pwd); cellViewHolder.tv_Phone = (TextView)v.findViewById(R.id.tv_phone); // tip,通过tag绑定 v.setTag(cellViewHolder); } cellViewHolder.tv_Name.setText(p.getName()); cellViewHolder.tv_Pwd.setText(p.getPwd()); cellViewHolder.tv_Phone.setText(p.getPhone()); return v; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } } // 静态内部类,免去每次查找的烦恼,提升5%性能 private static class CellViewHolder {private TextView tv_Name;private TextView tv_Pwd;private TextView tv_Phone;} }
1 0
- android_20_ListView_BaseAdapter
- 学习ardupilot一点心得
- orcale小白学习的艰辛历程
- window系统搭建git服务器-Gitblit
- JavaScript 关于不同类型宽度和高度的区分
- 18. 使用模板【从零开始学Spring Boot】
- android_20_ListView_BaseAdapter
- 快速行驶的自动驾驶,离现实还有多远?
- 程序员面试金典: 9.4树与图 4.3给定一个有序整数数组,元素各不相同且按升序排列,创建一颗高度最小的二叉查找树。 ---快速解法
- SpringMVC常用获取传递参数的方法
- @Field parameters can only be used with form encoding
- SQL Server 存储(2/8):理解数据记录结构
- 19. Spring Boot 添加JSP支持【从零开始学Spring Boot】
- AST
- gcc4.4.7升级到gcc4.8.0