列出所有记录j2me

时间:2011-11-02 01:13:52

标签: java-me midp lcdui rms

我想列出记录存储中的所有记录。我已经宣布了一个清单:

Private List tlist = new List("Select One", List.IMPLICIT);

还列举了记录:

Form mainf;
Command exit = new Command("Exit", Command.EXIT, 0);
RecordEnumeration re = null;
int numrecords = 0;
    try {
        re = contactList.enumerateRecords(null, null, false);
        numrecords = re.numRecords();
    } catch (RecordStoreException rse ) { numrecords = 0;}

现在我只需要知道如何列出列表中的所有记录。

2 个答案:

答案 0 :(得分:3)

Record添加到Vector并使用该循环将Vector值附加到List。见下面的示例代码,

RecordStore s =  RecordStore.openRecordStore(recordStoreName, true);
RecordEnumeration e = s.enumerateRecords(null, null, false);
Vector vector = new Vector();
while(e.hasNextElement()) {
  vector.addElement(new String(e.nextRecord()));
}
List l = new List(null, CENTER);
for (int i = 0; i < vector.size(); i++) {
  l.append(vector.elementAt(i).toString(), null); 
}

答案 1 :(得分:1)

您可以使用以下代码

public String [] getRecordData()
{
    String[] str = null;
    int counter = 0;
    try 
    {
        RecordEnumeration enumeration = rs.enumerateRecords(null, null, false);
        str = new String[rs.getNumRecords()];

        while(enumeration.hasNextElement())
        {
            try 
            {
                str[counter] = (new String(enumeration.nextRecord()));
                counter ++;
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return str;
}