Android的SQlite先天不足:删除 插入后主键不能自动排序 解决(附:SQlite开发的完整demo)

  SQlite有个问题,就是主键不能够自动排序。比如说主键id为1 2 3 4,共4条记录。现在删除2 3,还剩下1 4记录,当再次插入时,id会变成5,而不是2.假设在初始4条记录的基础上,把这4条记录全都删掉,再次插入时,得到的id是5. 这种机制实在是太不好了!解决方法是在主键id外新增加一个realid,对realid进行处理。具体请看代码:

     这里一并附上

Android开发:setContentView切换界面,自定义带CheckBox的ListView显示SQlite条目-----实现

的代码。整个代码的功能是数据库里有个表,结构是id和姓名。可以增加、删除、更新,在主界面里有一个选定按钮,点击后会显示带checkbox的listview,显示数据库里的条目,按上面的确定按钮返回主界面。

第一部分:-------main.xml

[html]  view plain copy print ?
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="@android:color/white"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <EditText  
  9.         android:id="@+id/nameEdit"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:focusable="true"  
  13.         android:hint="请输入志愿者姓名"  
  14.         android:textColor="@android:color/holo_red_dark"  
  15.         android:textSize="25dip" />  
  16.   
  17.    <Button  
  18.        android:id="@+id/choseBtn"  
  19.        android:layout_width="fill_parent"  
  20.        android:layout_height="wrap_content"  
  21.        android:text="选定"  
  22.        />  
  23.   
  24.     <ListView  
  25.         android:id="@+id/palmList"  
  26.         android:layout_width="fill_parent"  
  27.         android:layout_height="wrap_content" >  
  28.     </ListView>  
  29.   
  30. </LinearLayout>  
  31. </span>  


 

my_checkbox.xml-----------用来控制listview的每一行怎么显示,这是跳转到选定界面呈现的

[html]  view plain copy print ?
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"     
  5.     android:orientation="horizontal" >  
  6.     <TextView   
  7.         android:id="@+id/item_text"  
  8.         android:textSize="25dip"  
  9.         android:layout_weight="1"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"/>  
  12.     <CheckBox   
  13.         android:id="@+id/item_check"  
  14.         android:textSize="25dip"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:clickable="false"  
  18.         android:focusable="false"  
  19.         android:checkMark="?android:attr/listChoiceIndicatorMultiple"/>  
  20.   
  21.   
  22.   
  23. </LinearLayout>  
  24. </span>  


 

list_check.xml-----------------------这个布局是主界面要跳转的界面的布局,上面是个按键,下面是个带checkbox的 listview

[html]  view plain copy print ?
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/confirmBtn"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="确定" />  
  12.   
  13.     <ListView  
  14.         android:id="@+id/checkList"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content" >  
  17.     </ListView>  
  18.   
  19. </LinearLayout>  
  20. </span>  


 

 

第二部分:

PalmDB.java--------------自定义的一个数据库类,继承SQLiteOpenHelper,功能有增加、删除、更新。

[java]  view plain copy print ?
  1. <span style="font-size:18px;">package yan.guoqi.testsqlite;  
  2.   
  3. import android.content.ContentValues;  
  4. import android.content.Context;  
  5. import android.database.Cursor;  
  6. import android.database.sqlite.SQLiteDatabase;  
  7. import android.database.sqlite.SQLiteDatabase.CursorFactory;  
  8. import android.database.sqlite.SQLiteOpenHelper;  
  9. import android.util.Log;  
  10.   
  11. public class PalmDB extends SQLiteOpenHelper{  
  12.   
  13.     private static final String DATABASE_NAME = "Palm.db";  
  14.     private static final int DATABASE_VERSION = 1;  
  15.     private static final String TABLE_NAME = "Palm_table";  
  16.       
  17.     //表里面的三个内容  
  18.     private static final String ID = "_id";  
  19.     private static final String NAME = "_name";  
  20.     private static final String RealID = "_realid";  
  21.       
  22.     private static final String Tag = "PalmDB";  
  23.   
  24.   
  25.   
  26.     public PalmDB(Context context) //, String name, CursorFactory factory,  int version  
  27.     {  
  28.         super(context, DATABASE_NAME, null, DATABASE_VERSION);  
  29.         // TODO Auto-generated constructor stub  
  30.     }  
  31.   
  32.     @Override  
  33.     public void onCreate(SQLiteDatabase db) {  
  34.         // TODO Auto-generated method stub  
  35.         String sql = "CREATE TABLE " + TABLE_NAME + " (" + ID + " INTEGER primary key autoincrement, "  
  36.                 + NAME +" text, " + RealID + " INTEGER);"//  
  37.         db.execSQL(sql);  
  38.           
  39.     }  
  40.   
  41.     @Override  
  42.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  43.         // TODO Auto-generated method stub  
  44.         String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;  
  45.         db.execSQL(sql);  
  46.         onCreate(db);  
  47.     }  
  48.       
  49.       
  50.     public Cursor select(){       
  51.         SQLiteDatabase db = this.getReadableDatabase();  
  52.         Cursor cursor = db.query(TABLE_NAME, nullnullnullnullnull, RealID +" ASC");  
  53.         return cursor;  
  54.           
  55.     }  
  56.       
  57.       
  58.     /*增加操作*/  
  59.     public long insert(String name){  
  60.         SQLiteDatabase db = this.getWritableDatabase();  
  61.         ContentValues cv = new ContentValues();  
  62.         int realid = getRealId();  
  63.         cv.put(NAME, name);  
  64.         cv.put(RealID, realid);  
  65.         long row = db.insert(TABLE_NAME, null, cv);  
  66.         return realid;  
  67.         //return row;  //row是行的意思  
  68.           
  69.     }  
  70.       
  71.     /*得到一个真实的id*/  
  72.     public int getRealId(){  
  73.         Cursor cursor = select();  
  74.         int realid = 1;  
  75.           
  76.         //如果cursor为空,返回id = 1  
  77.         if(!cursor.moveToFirst()){  
  78.             return realid;  
  79.         }  
  80.         else{  
  81.             while(true){  
  82.                 if(realid != cursor.getInt(2))  
  83.                 {  
  84.                     return realid;  
  85.                 }  
  86.                 else  
  87.                 {  
  88.                     realid++;  
  89.                     if(!cursor.moveToNext())  
  90.                         return realid;  
  91.                 }  
  92.             }  
  93.         }  
  94.     }  
  95.       
  96.     /*删除操作*/  
  97.     public void delete(int id){  
  98.         SQLiteDatabase db = this.getWritableDatabase();  
  99.         String where = ID + "=?";  
  100.         String[] whereValue = { Integer.toString(id) };  
  101.         db.delete(TABLE_NAME, where, whereValue);  
  102.           
  103.     }  
  104.       
  105.     /*修改操作*/  
  106.     //id是你要修改的id号,name是新的名字  
  107.     public void update(int id, String name){  
  108.         SQLiteDatabase db = this.getWritableDatabase();  
  109.         String where = ID + "=?";  
  110.         String[] whereValue = { Integer.toString(id) };  
  111.   
  112.         ContentValues cv = new ContentValues();  
  113.         cv.put(NAME, name);  
  114.           
  115.         db.update(TABLE_NAME, cv, where, whereValue);  
  116.     }  
  117.       
  118.   
  119. }  
  120. </span>  


需要注意的几点:

1,这个表里有3个元素,分别是ID NAME  RealID ,这个ID是主键,类型是INTEGER primary key autoincrement, 第三个RealID是为了解决主键不能自动排序而新增加的。

2,public void onCreate(SQLiteDatabase db)这个函数是数据库创建的时候,注意这句话的写法:

String sql = "CREATE TABLE " + TABLE_NAME + " (" + ID + " INTEGER primary key autoincrement, "

                            + NAME +" text, " + RealID + " INTEGER);"; //

里面的标点可是一点不敢错,每个元素后面带“,”。

3,public Cursor select()这个函数很关键,是获得查询的游标。Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, RealID +" ASC"); 后面的最后一个参数是让查询的时候按照RealID升序排列。这个很关键,这和下面的自动产生排序的ID亲密相连。

4,public int getRealId()这个函数得到自动排序的ID。里面就是算法:

[java]  view plain copy print ?
  1. <span style="font-size:18px;">/*得到一个排好序,即将插入的id*/  
  2.     public int getRealId(){  
  3.         Cursor cursor = select();  
  4.         int realid = 1;  
  5.           
  6.         //如果cursor为空,返回id = 1  
  7.         if(!cursor.moveToFirst()){  
  8.             return realid;  
  9.         }  
  10.         else{  
  11.             while(true){  
  12.                 if(realid != cursor.getInt(2))  
  13.                 {  
  14.                     return realid;  
  15.                 }  
  16.                 else  
  17.                 {  
  18.                     realid++;  
  19.                     if(!cursor.moveToNext())  
  20.                         return realid;  
  21.                 }  
  22.             }  
  23.         }  
  24.     }  
  25. </span>  


    这个函数是让id自动排序的关键。首先判断cursor是否为空,空的话就是没有记录,则返回的id = 1; 不空的话则进入循环。如果realid不等于当前cursor的第一行元素,则直接返回realid。否则话realid加1,游标往下移一行。这里跟插入法排序有点像,前提是这个游标必须是有序的。这就和上面的3挂上钩了。可以用Java,借助数组模拟验证这个算法是否对:

[java]  view plain copy print ?
  1. <span style="font-size:18px;">package yan.guoqi;  
  2.   
  3. public class MainClass {  
  4.     public static void main(String[] args){  
  5.   
  6.         int a = MainClass.getRealId();   
  7.         System.out.println(" "+ a);  
  8.   
  9.   
  10.   
  11.     }  
  12.     /*得到真正的real—id*/  
  13.     public static  int getRealId(){  
  14.         int[] cursor = {136};  
  15.         int id=1;  
  16.         int i=0;  
  17.         while(true){  
  18.             if(cursor.length == 0)  
  19.                 return id;  
  20.             else{  
  21.                 if(id!=cursor[i])  
  22.                 {  
  23.                     return id;  
  24.                 }  
  25.                 else  
  26.                 {  
  27.                     id++;  
  28.                     i++;  
  29.                     if(i == cursor.length)  
  30.                         return id;  
  31.                 }  
  32.             }  
  33.         }  
  34.   
  35.     }  
  36.   
  37.   
  38.   
  39. }  
  40. </span>  
[java]  view plain copy print ?
  1. <span style="font-size:18px;"></span>   
[java]  view plain copy print ?
  1. <span style="font-size:18px;BACKGROUND-COLOR: #ffffff"></span>   

 

5,至于下面的delete和update函数,是删除和更新,依据的索引是 ID,而不是RealID。 当然如果你喜欢,改成依据RealID也无妨。 详细可参考:

Android 高手进阶教程(十三)之----Android 数据库SQLiteDatabase的使用!!

 

 

6,另外就是insert函数,返回的是realid,也就是你插入一条记录后,这个记录的真实行号。这个值和long row = db.insert(TABLE_NAME, null, cv);里的row是不同的,这个row是ID号。是不规则的那个行号,依据主键而来

 

 第三部分:主界面的源码

[java]  view plain copy print ?
  1. <span style="font-size:18px;">package yan.guoqi.testsqlite;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import yan.guoqi.testsqlite.TestSQliteActivity.CheckListAdapter.ViewHolder;  
  7.   
  8. import android.app.Activity;  
  9. import android.content.Context;  
  10. import android.database.Cursor;  
  11. import android.graphics.Color;  
  12. import android.os.Bundle;  
  13. import android.util.Log;  
  14. import android.view.LayoutInflater;  
  15. import android.view.Menu;  
  16. import android.view.MenuItem;  
  17. import android.view.View;  
  18. import android.view.View.OnClickListener;  
  19. import android.view.ViewGroup;  
  20. import android.view.WindowManager;  
  21. import android.widget.AdapterView;  
  22. import android.widget.AdapterView.OnItemClickListener;  
  23. import android.widget.BaseAdapter;  
  24. import android.widget.Button;  
  25. import android.widget.CheckBox;  
  26. import android.widget.EditText;  
  27. import android.widget.ListView;  
  28. import android.widget.TextView;  
  29. import android.widget.Toast;  
  30.   
  31. public class TestSQliteActivity extends Activity implements AdapterView.OnItemClickListener {  
  32.   
  33.     private PalmDB mPalmDB;  
  34.     private Cursor cursor;  
  35.     private EditText mNameEdit;  
  36.     private ListView mPalmList;  
  37.   
  38.     private int id = 0;  
  39.     protected static final int MENU_ADD = Menu.FIRST;  
  40.     protected static final int MENU_DELETE = Menu.FIRST + 1;  
  41.     protected static final int MENU_UPDATE= Menu.FIRST + 2;  
  42.   
  43.     private static final String Tag="SQlite:";  
  44.   
  45.     //为了实现新的布局  
  46.     Button mChoseBtn = null;  
  47.     Button mConfirmBtn = null;  
  48.   
  49.     boolean firstFlag = true;  
  50.     ListView list2 = null;  
  51.     View checkListView = null;  
  52.     View mainView = null;  
  53.     /** Called when the activity is first created. */  
  54.     @Override  
  55.     public void onCreate(Bundle savedInstanceState) {  
  56.         super.onCreate(savedInstanceState);  
  57.   
  58.           
  59.           
  60.         LayoutInflater inflater = this.getLayoutInflater();  
  61.         checkListView = inflater.inflate(R.layout.list_check, null);  
  62.         mainView = inflater.inflate(R.layout.main, null);  
  63.   
  64.   
  65.   
  66.         setContentView(mainView);  
  67.         //切换布局监听  
  68.         mChoseBtn = (Button)mainView.findViewById(R.id.choseBtn);  
  69.         mChoseBtn.setOnClickListener(new ButtonListener());  
  70.           
  71.         setUpViews();  
  72.   
  73.   
  74.     }  
  75.   
  76.     class ButtonListener implements OnClickListener{  
  77.   
  78.         public void onClick(View v) {  
  79.             // TODO Auto-generated method stub  
  80.             switch (v.getId()){  
  81.             case R.id.choseBtn:  
  82.           
  83.                 Jump2CheckList();  
  84.                 break;  
  85.             case R.id.confirmBtn:  
  86.                 String s = getCheckInfo();  
  87.                 showToast("您选中的姓名有:"+ s);  
  88.                 Jump2Main();  
  89.                 break;  
  90.             default:  
  91.                 break;  
  92.             }  
  93.         }  
  94.   
  95.     }  
  96.   
  97.     public String getCheckInfo()  
  98.     {  
  99.         String info = "";  
  100.         for(int i=0; i<list2.getCount(); i++){  
  101.             if(isSelected.get(i)){  
  102.                 //ViewHolder holder = (ViewHolder)list2.getChildAt(i).getTag();  
  103.                 cursor.moveToPosition(i);  
  104.                 info+=cursor.getInt(0)+".";  
  105.             }  
  106.         }  
  107.         return info;  
  108.     }  
  109.     /*切换到选中布局*/  
  110.     public void Jump2CheckList(){  
  111.         setContentView(checkListView);  
  112.         if(firstFlag){  
  113.             mConfirmBtn = (Button)checkListView.findViewById(R.id.confirmBtn);  
  114.             mConfirmBtn.setOnClickListener(new ButtonListener());  
  115.             firstFlag = false;  
  116.         }  
  117.         initCheckList();  
  118.   
  119.   
  120.     }  
  121.   
  122.   
  123.     public void initCheckList(){  
  124.         list2 = (ListView)(checkListView).findViewById(R.id.checkList);  
  125.         list2.setItemsCanFocus(false);  
  126.         list2.setAdapter(new CheckListAdapter(this, cursor));  
  127.         list2.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);  
  128.         list2.setOnItemClickListener(new OnItemClickListener() {  
  129.   
  130.             public void onItemClick(AdapterView<?> arg0, View view, int positon,  
  131.                     long id) {  
  132.                 // TODO Auto-generated method stub  
  133.                 ViewHolder vHolder = (ViewHolder) view.getTag();  
  134.                 vHolder.check.toggle();  
  135.                 isSelected.put(positon, vHolder.check.isChecked());  
  136.                   
  137.                   
  138.             }  
  139.         });  
  140.     }  
  141.   
  142.   
  143.   
  144.     /*切换到主布局*/  
  145.     public void Jump2Main(){  
  146.         setContentView(mainView);         
  147.         setUpViews();  
  148.     }  
  149.   
  150.     /*初始化数据库,更新View*/  
  151.     public void setUpViews(){  
  152.           
  153.         //禁止输入法自己探出来  
  154.         this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);  
  155.         mPalmDB = new PalmDB(this);       
  156.         Log.i(Tag, "aaaaaaaaa");  
  157.         cursor = mPalmDB.select();  
  158.         Log.i(Tag, "11111111");  
  159.   
  160.         mNameEdit = (EditText)findViewById(R.id.nameEdit);  
  161.         mPalmList = (ListView)findViewById(R.id.palmList);  
  162.   
  163.         mPalmList.setAdapter(new PalmListAdapter(this, cursor));  
  164.         mPalmList.setOnItemClickListener(this);  
  165.   
  166.     }  
  167.   
  168.   
  169.   
  170.     @Override  
  171.     public boolean onCreateOptionsMenu(Menu menu) {  
  172.         // TODO Auto-generated method stub  
  173.         super.onCreateOptionsMenu(menu);  
  174.         //menu.  
  175.         menu.add(Menu.NONE, MENU_ADD, 0"增加");  
  176.         menu.add(Menu.NONE, MENU_DELETE, 1"删除");  
  177.         menu.add(Menu.NONE, MENU_UPDATE, 2"修改");  
  178.   
  179.         return true;  
  180.   
  181.     }  
  182.   
  183.   
  184.   
  185.     @Override  
  186.     public boolean onOptionsItemSelected(MenuItem item) {  
  187.         // TODO Auto-generated method stub  
  188.         super.onOptionsItemSelected(item);  
  189.         switch(item.getItemId()){  
  190.         case MENU_ADD:  
  191.             add();  
  192.             break;  
  193.         case MENU_DELETE:  
  194.             delete();  
  195.             break;  
  196.         case MENU_UPDATE:  
  197.             update();  
  198.             break;  
  199.         default:  
  200.             break;  
  201.         }  
  202.         return true;  
  203.   
  204.     }  
  205.   
  206.     public void add(){  
  207.         String name = mNameEdit.getText().toString();  
  208.         if(name.equals("") ){  
  209.             showToast("志愿者姓名不能为空");  
  210.             return;  
  211.         }  
  212.         long row = mPalmDB.insert(name);  
  213.         cursor.requery();  
  214.         mPalmList.invalidateViews();  
  215.         mNameEdit.setText("");  
  216.         showToast("添加成功!编号:" + row);// + Integer.toString(cursor.getInt(0))  
  217.   
  218.     }  
  219.   
  220.     public void delete(){  
  221.         if(id == 0){  
  222.             showToast("还未有志愿者,无法删除!");  
  223.             return ;  
  224.         }  
  225.         if(mNameEdit.getText().toString().equals("")){  
  226.             showToast("请选中后删除!");  
  227.             return;  
  228.         }  
  229.         mPalmDB.delete(id);  
  230.         cursor.requery();  
  231.         mPalmList.invalidateViews();  
  232.         mNameEdit.setText("");  
  233.         showToast("删除成功!");  
  234.   
  235.     }  
  236.   
  237.     public void update(){  
  238.         String name = mNameEdit.getText().toString();  
  239.         if(name.equals("")) {  
  240.             showToast("选中为空!无法修改。");  
  241.             return;  
  242.         }  
  243.         mPalmDB.update(id, name);  
  244.         cursor.requery();  
  245.         mPalmList.invalidateViews();  
  246.         mNameEdit.setText("");  
  247.         showToast("修改成功!");  
  248.   
  249.   
  250.   
  251.     }  
  252.   
  253.     public void onItemClick(AdapterView<?> arg0, View view, int position, long _id) {  
  254.         // TODO Auto-generated method stub  
  255.         cursor.moveToPosition(position);  
  256.         id = cursor.getInt(0);  
  257.         mNameEdit.setText(cursor.getString(1));  
  258.   
  259.     }  
  260.   
  261.     public void showToast(String str){  
  262.         Toast.makeText(this,  
  263.                 str,  
  264.                 Toast.LENGTH_SHORT).show();  
  265.     }  
  266.   
  267.   
  268.     /*给CheckList设置适配器*/  
  269.     public static  Map<Integer, Boolean> isSelected;  
  270.     public  class CheckListAdapter extends BaseAdapter{  
  271.   
  272.         private Context mContext;  
  273.         private Cursor mCursor;  
  274.           
  275.   
  276.         //构造函数  
  277.         public CheckListAdapter(Context context, Cursor cursor){  
  278.             mContext = context;  
  279.             mCursor = cursor;  
  280.               
  281.             isSelected = new HashMap<Integer, Boolean>();  
  282.             for(int i=0; i<mCursor.getCount(); i++){  
  283.                 isSelected.put(i, false);  
  284.             }  
  285.         }  
  286.   
  287.   
  288.   
  289.         public int getCount() {  
  290.             // TODO Auto-generated method stub  
  291.             return cursor.getCount();  
  292.         }  
  293.   
  294.         public Object getItem(int arg0) {  
  295.             // TODO Auto-generated method stub  
  296.             return null;  
  297.         }  
  298.   
  299.         public long getItemId(int arg0) {  
  300.             // TODO Auto-generated method stub  
  301.             return 0;  
  302.         }  
  303.   
  304.         public View getView(int position, View convertView, ViewGroup arg2) {  
  305.             // TODO Auto-generated method stub  
  306.             ViewHolder holder = null;  
  307.             if(convertView == null){  
  308.                 holder = new ViewHolder();  
  309.                 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  310.                 convertView = inflater.inflate(R.layout.my_checkbox, null);  
  311.                 holder.text = (TextView) convertView.findViewById(R.id.item_text);  
  312.                 holder.check = (CheckBox)convertView.findViewById(R.id.item_check);  
  313.                 convertView.setTag(holder);  
  314.             }  
  315.             else  
  316.             {  
  317.                 holder = (ViewHolder)convertView.getTag();  
  318.             }  
  319.           
  320.               
  321.             mCursor.moveToPosition(position);  
  322.             holder.text.setText(Integer.toString(mCursor.getInt(2)));  
  323.             holder.text.append(mCursor.getString(1));  
  324.             holder.check.setChecked(isSelected.get(position));  
  325.             return convertView;  
  326.   
  327.         }  
  328.           
  329.         public final class ViewHolder{  
  330.             public TextView text;  
  331.             public CheckBox check;  
  332.         }  
  333.   
  334.     }  
  335.   
  336.   
  337.   
  338.     /*自定义适配器*/  
  339.     public class PalmListAdapter extends BaseAdapter{  
  340.   
  341.         private Context mContext;  
  342.         private Cursor mCursor;  
  343.   
  344.         //构造函数  
  345.         public PalmListAdapter(Context context, Cursor cursor){  
  346.             mContext = context;  
  347.             mCursor = cursor;  
  348.         }  
  349.         public int getCount() {  
  350.             // TODO Auto-generated method stub  
  351.             return mCursor.getCount();  
  352.         }  
  353.   
  354.         public Object getItem(int arg0) {  
  355.             // TODO Auto-generated method stub  
  356.             return null;  
  357.         }  
  358.   
  359.         public long getItemId(int arg0) {  
  360.             // TODO Auto-generated method stub  
  361.             return 0;  
  362.         }  
  363.   
  364.         public View getView(int position, View convertView, ViewGroup parent) {  
  365.             // TODO Auto-generated method stub  
  366.             TextView mTextView = new TextView(mContext);  
  367.             mCursor.moveToPosition(position);  
  368.             mTextView.setTextSize(25);  
  369.             mTextView.setTextColor(Color.BLUE);  
  370.             mTextView.setText(Integer.toString(mCursor.getInt(2)));  
  371.             mTextView.append(mCursor.getString(1));  
  372.             return mTextView;  
  373.         }  
  374.   
  375.     }  
  376. }  
  377. </span>  

 

注意:

1,程序基本上和上篇

Android开发:setContentView切换界面,自定义带CheckBox的ListView显示SQlite条目-----实现

差不多。可以看到适配器其实很好写,CheckListAdapter是带checkbox的listview的适配器, PalmListAdapter是主界面的那个适配器。 适配器的重点在public View getView(int position, View convertView, ViewGroup parent)这个函数,可以对比两个适配器在获得view上写法上的不同。

 

2,由于在源程序2里,删除依据的id是ID,也就是主键,所以在这里提供的也必须是主ID。 如果一个用ID 一个提供的是RealID,会出现删除不了,删除错乱的情况!

 

 

最后看看效果:

初始界面:

 

增加4条记录后:

 

删除2 和3:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值