封装RecordStore让其记录自动紧凑

        因为RecordStore在删除一条纪录后,纪录的位置仍然保留,并不会自动紧凑。这可能也是sun为了效率而故意做成这样的。不过在我们进行记录的删除操作时,实在是太不方便了。因为很难计算出记录的RecordID。

       下面是我封装的一个方法,让RecordStore自动紧凑。

 

       public void delete(String recordName,int recordIndex){ 
    
                                         recordDB.open("recordName");
                                         recordDB.deleteRecord(recordIndex);
    
                                        int tempRecordCount = recordDB.getNumRecords()+1;
                                        RecordDB tempRecordDB = new RecordDB();
                                        tempRecordDB.open("newsLoader");
    
                                        for(int i = 1;i <=tempRecordCount;i++){
                                                  if(i != NewsContentCanvas.tempIndex+1){
                                                          String tempString = recordDB.getRecordAndID(i);

                                                           tempRecordDB.addRecord(tempString.substring(0,tempString.indexOf("*")),Integer.parseInt(tempString.substring(tempString.indexOf("*")+1)));
     }
    }
    
    recordDB.close();
    RecordDB.deleteStore("picturesRecord");
    
    recordDB.open("picturesRecord");
    for(int i = 1;i <=tempRecordDB.getNumRecords();i++){
     String tempString = tempRecordDB.getRecordAndID(i);
     recordDB.addRecord(tempString.substring(0,tempString.indexOf("*")),Integer.parseInt(tempString.substring(tempString.indexOf("*")+1)));
    }
    
    recordDB.close();
    tempRecordDB.close();
    RecordDB.deleteStore("newsLoader");
   } 



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1176240


你可能感兴趣的:(store)