大家应该都有过一个想法,就是用listview结合自定义的Adapter来写一个结合SQLite数据库的工程~现在就写下了的哈~~~
第一步:编写关于数据库的类和方法 DBHelper.java
代码如下:
package com.jftt.db; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import com.jftt.conmmon.CONST; public class DBHelper extends SQLiteOpenHelper { private static final String TAG = "DBHelper"; public DBHelper(Context context) { super(context, CONST.JFTT_DATABASE_NAME, null, CONST.JFTT_DATABASE_VERSION); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { String createTableSql = "create table " + CONST.JFTT_DATABASE_TABLE_USER + "(_id autoinc primary key , u_name varchar(20),u_pass varchar(20))"; db.execSQL(createTableSql); Log.d(TAG, "Table " + CONST.JFTT_DATABASE_TABLE_USER + " createad succesfully"); createTableSql = "CREATE TABLE " + CONST.JFTT_DATABASE_TABLE_INFO + "(" + "_id AUTOINC PRIMARY KEY," + "u_age VARCHAR(20)NOT NULL ON CONFLICT FAIL," + "u_addr VARCHAR(20)NOT NULL ON CONFLICT FAIL," + "u_email VARCHAR(20)NOT NULL ON CONFLICT FAIL )"; db.execSQL(createTableSql); Log.d(TAG, "Database " + CONST.JFTT_DATABASE_NAME + " createad succesfully"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub String dropTableSQL = "DROP TABLE IF EXISTS " + CONST.JFTT_DATABASE_TABLE_INFO + " "; db.execSQL(dropTableSQL); dropTableSQL = "DROP TABLE IF EXISTS " + CONST.JFTT_DATABASE_TABLE_USER + " "; db.execSQL(dropTableSQL); onCreate(db); } // execute insert,update,delete and so on.. public void execSQL(String sql, Object[] args) { SQLiteDatabase db = this.getWritableDatabase(); db.execSQL(sql, args); Log.d(TAG, "Execute SQL " + sql + " succesfully"); } public Cursor query(String sql, String[] args) { SQLiteDatabase db = this.getWritableDatabase(); Log.d(TAG, "Query SQL " + sql + " being excuted...."); Cursor cursor = db.rawQuery(sql, args); return cursor; } }
表中结构其实很简单:
我们建了两个表:
jftt_user表
jftt_info
由于程序是练习使用,我们没有用到表 jftt_user(呵呵,其实不见这个表也可以的哈)
2:创建应用程序的Adapter(CursorAdapter.java)
代码如下:
package com.jftt.adpater; import com.jftt.sqlite.R; import android.content.Context; import android.database.Cursor; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class CursorAdapter extends BaseAdapter { private static final String TAG = "CursorAdapter"; private Context context; private Cursor cursor; private LayoutInflater inflater; private LinearLayout pageLayout; public CursorAdapter(Context context, Cursor cursor) { super(); this.context = context; this.cursor = cursor; inflater = LayoutInflater.from(context); } @Override public int getCount() { Log.d(TAG, "Count :" + cursor.getCount()); // TODO Auto-generated method stub return cursor.getCount(); } @Override public Object getItem(int position) { Log.d(TAG, "Item :" + position); // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub Log.d(TAG, "ItemId :" + position); return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { cursor.moveToPosition(position); // TODO Auto-generated method stub pageLayout = (LinearLayout) inflater.inflate(R.layout.page, null); TextView textAddr = (TextView) pageLayout.findViewById(R.id.TextView01); TextView textEmail = (TextView) pageLayout .findViewById(R.id.TextView02); textAddr.setText(cursor.getString(2)); Log.d(TAG, "Addr :" + cursor.getString(2)); textEmail.setText(cursor.getString(3)); ImageView image = (ImageView) pageLayout.findViewById(R.id.ImageView01); image.setImageDrawable(context.getResources().getDrawable( R.drawable.icon0)); return pageLayout; } }
程序简单,用心的同学不介意没有注释就懂得哈。我们在程序中加载一个布局文件,用于listview中的每一行信息,布局文件page.xml如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/ImageView01" android:paddingTop="10px" android:layout_marginRight="10px" android:layout_width="50dip" android:layout_height="50dip"></ImageView> <LinearLayout android:id="@+id/layout_linear" android:orientation="vertical" android:layout_toRightOf="@id/ImageView01" android:layout_width="300px" android:layout_height="wrap_content"> <TextView android:text="@+id/TextView01" android:id="@+id/TextView01" android:layout_below="@id/ImageView01" android:layout_width="wrap_content" android:layout_marginTop="5px" android:layout_marginBottom="10px" android:textColor="@color/blue" android:layout_height="wrap_content"> </TextView> <TextView android:text="@+id/TextView02" android:id="@+id/TextView02" android:layout_marginTop="10px" android:textColor="@color/blue" android:layout_below="@id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </LinearLayout> </RelativeLayout> </LinearLayout>
注意---- cursor.moveToPosition(position);当我们要改变ListView中每一行的数据时候,游标是不断改变的,就是移动到要显示的View的position处,这一点我搞了好久,原以为只是顺序的移动带下一个行就行了。但是实践证明我们必须这样写。
3:编写程序入口(主Activity)
代码如下:SQLiteActivity.java
package com.jftt.sqlite; import com.jftt.adpater.CursorAdapter; import com.jftt.conmmon.CONST; import com.jftt.db.DBHelper; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.util.Log; import android.widget.ListView; import android.widget.SimpleCursorAdapter; public class SQLiteActivity extends Activity { private static final String TAG = "SQLiteActivity"; private ListView listView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { Log.d(TAG, "App started"); super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); DBHelper dbHelper = new DBHelper(this); Cursor cursor = dbHelper.query("select * from " + CONST.JFTT_DATABASE_TABLE_INFO, null); listView.setAdapter(new CursorAdapter(this, cursor)); } private void init() { Log.d(TAG, "Initialing UI Component"); this.listView = (ListView) this.findViewById(R.id.ListView01); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:background="@drawable/background" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <ListView android:layout_width="wrap_content" android:dividerHeight="0.5dip" android:scrollX="10px" android:divider="#4169E1" android:layout_height="wrap_content" android:id="@+id/ListView01" /> </LinearLayout>
程序中还会用到一些华而不是的技巧,其中需要以下文件,写在drawable下面:
background.xml
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ff4100ff"/> <gradient android:startColor="#ff8c00" android:endColor="#FFFFFF" android:angle="270" /> <stroke android:width="2dp" android:color="#dcdcdc" /> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape>
不见不足以信服,好吧,看看效果图就知道了哈: