×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    1

    Post RMS record search and to place in an array

    hi all,
    I have a problem in getting the retreived values from the record store and place it in an array.

    The code is as follows:

    public void append(String charText)
    {
    if(item>1)
    this.items.append(charText+"*");
    else
    this.items.append(charText);
    }

    public void openRecStore()
    {
    try
    {
    // The second parameter indicates that the record store
    // should be created if it does not exist
    rs = RecordStore.openRecordStore("All Items", true );
    }
    catch (Exception e)
    {
    System.out.println("Exception in opening: "+e.getMessage());
    }
    }

    public void closeRecStore()
    {
    try
    {
    rs.closeRecordStore();
    }
    catch (Exception e)
    {
    System.out.println("Exception in closing record: "+e.getMessage());
    }
    }

    /*--------------------------------------------------
    * Create array of data to write into record store
    *-------------------------------------------------*/
    public void writeTestData()
    {
    String[] items = {
    "bag101-Laptop Bag-Rs.600",
    "bag102-Travel Bag-Rs.500",
    "bag103-School Bag-Rs.350",
    "wal201-Leather Wallet-Rs.400",
    "wal202-Trifold Wallet-Rs.450",
    "wal203-Double Slimfold Wallet-Rs.300",
    "sht501-Formal Shirts-Rs.1500",
    "sht502-Casual Shirts-Rs.1050",
    "sht503-Kids Shirts-Rs.700"};

    writeRecords(items);
    }

    private void writeRecords(String[] items) {
    byte[] record;

    try
    {
    // Only add the records once
    if (rs.getNumRecords() > 0)
    return;

    for (int i = 0; i < items.length; i++)
    {
    record = items[i].getBytes();
    rs.addRecord(record, 0, record.length);
    }
    }
    catch (Exception e)
    {
    System.out.println("Exception in writing records: "+e.getMessage());
    }
    }

    /*--------------------------------------------------
    * Search using enumerator and record filter
    *-------------------------------------------------*/
    public void searchRecordStore()
    {
    int i =0;

    try
    {
    System.out.println("Number of records: "+rs.getNumRecords());
    // Record store is not empty
    if (rs.getNumRecords() > 0)
    {
    // Setup the search filter with the user requested text
    SearchFilter search = new SearchFilter(msc.code);

    RecordEnumeration re = rs.enumerateRecords(search, null, false);


    // A match was found using the filter
    if (re.numRecords() > 0)
    {

    while (re.hasNextElement()) {
    int id = re.nextRecordId();
    System.out.println("Record ID: " + id);
    String str2 = null;
    str2 = new String(rs.getRecord(id));
    item++;
    append(str2);------->i have a problem here, i am unable to place in an array
    //System.out.println("Values which start with" + str2);//+" are "+str1[i]);
    //searchResult+=str2;
    //System.out.println("Search Results: "+searchResult);

    }


    }

    re.destroy(); // Free enumerator
    }
    itemDetails=items.toString();
    System.out.println("Items are: "+items.toString());
    }
    catch (Exception e)
    {
    System.out.println("Exception in searching for a record: "+e.getMessage());
    }
    }

    /*--------------------------------------------------
    * Search for text within a record
    * Each record passed in contains only text (String)
    *-------------------------------------------------*/
    public class SearchFilter implements RecordFilter
    {
    private String searchText = null;
    private String searchNoResult = null;

    public SearchFilter(String searchText)
    {
    // This is the text to search for
    this.searchText = searchText.toLowerCase();
    }

    public boolean matches(byte[] candidate)
    {
    String str = new String(candidate).toLowerCase();

    // Look for a match
    if (searchText != null && str.startsWith(searchText))
    return true;
    else
    searchNoResult="No Item Found";
    return false;
    }
    }

    Please help me out

  2. #2
    Super Contributor
    Join Date
    Sep 2008
    Location
    Noida, U.P.
    Posts
    1,330

    Thumbs up Re: RMS record search and to place in an array

    Quote Originally Posted by udaykapavarapu View Post
    hi all,
    /*--------------------------------------------------
    * Search using enumerator and record filter
    *-------------------------------------------------*/
    public void searchRecordStore()
    {
    int i =0;

    try
    {
    System.out.println("Number of records: "+rs.getNumRecords());
    // Record store is not empty
    if (rs.getNumRecords() > 0)
    {
    // Setup the search filter with the user requested text
    SearchFilter search = new SearchFilter(msc.code);

    RecordEnumeration re = rs.enumerateRecords(search, null, false);


    // A match was found using the filter
    if (re.numRecords() > 0)
    {

    while (re.hasNextElement()) {
    int id = re.nextRecordId();
    System.out.println("Record ID: " + id);
    String str2 = null;
    str2 = new String(rs.getRecord(id));
    item++;
    append(str2);------->i have a problem here, i am unable to place in an array
    //System.out.println("Values which start with" + str2);//+" are "+str1[i]);
    //searchResult+=str2;
    //System.out.println("Search Results: "+searchResult);

    }


    }

    re.destroy(); // Free enumerator
    }
    itemDetails=items.toString();
    System.out.println("Items are: "+items.toString());
    }
    catch (Exception e)
    {
    System.out.println("Exception in searching for a record: "+e.getMessage());
    }
    }
    Please explain the bold lines.
    thanks,
    jitu_goldie..

    KEEP TRYING..

  3. #3
    Nokia Developer Champion
    Join Date
    Sep 2007
    Posts
    915

    Re: RMS record search and to place in an array

    Quote Originally Posted by udaykapavarapu View Post
    hi all,
    I have a problem in getting the retreived values from the record store and place it in an array.

    The code is as follows:

    public void append(String charText)
    {
    if(item>1)
    this.items.append(charText+"*");
    else
    this.items.append(charText);
    }

    public void openRecStore()
    {
    try
    {
    // The second parameter indicates that the record store
    // should be created if it does not exist
    rs = RecordStore.openRecordStore("All Items", true );
    }
    catch (Exception e)
    {
    System.out.println("Exception in opening: "+e.getMessage());
    }
    }

    public void closeRecStore()
    {
    try
    {
    rs.closeRecordStore();
    }
    catch (Exception e)
    {
    System.out.println("Exception in closing record: "+e.getMessage());
    }
    }

    /*--------------------------------------------------
    * Create array of data to write into record store
    *-------------------------------------------------*/
    public void writeTestData()
    {
    String[] items = {
    "bag101-Laptop Bag-Rs.600",
    "bag102-Travel Bag-Rs.500",
    "bag103-School Bag-Rs.350",
    "wal201-Leather Wallet-Rs.400",
    "wal202-Trifold Wallet-Rs.450",
    "wal203-Double Slimfold Wallet-Rs.300",
    "sht501-Formal Shirts-Rs.1500",
    "sht502-Casual Shirts-Rs.1050",
    "sht503-Kids Shirts-Rs.700"};

    writeRecords(items);
    }

    private void writeRecords(String[] items) {
    byte[] record;

    try
    {
    // Only add the records once
    if (rs.getNumRecords() > 0)
    return;

    for (int i = 0; i < items.length; i++)
    {
    record = items[i].getBytes();
    rs.addRecord(record, 0, record.length);
    }
    }
    catch (Exception e)
    {
    System.out.println("Exception in writing records: "+e.getMessage());
    }
    }

    /*--------------------------------------------------
    * Search using enumerator and record filter
    *-------------------------------------------------*/
    public void searchRecordStore()
    {
    int i =0;

    try
    {
    System.out.println("Number of records: "+rs.getNumRecords());
    // Record store is not empty
    if (rs.getNumRecords() > 0)
    {
    // Setup the search filter with the user requested text
    SearchFilter search = new SearchFilter(msc.code);

    RecordEnumeration re = rs.enumerateRecords(search, null, false);


    // A match was found using the filter
    if (re.numRecords() > 0)
    {

    while (re.hasNextElement()) {
    int id = re.nextRecordId();
    System.out.println("Record ID: " + id);
    String str2 = null;
    str2 = new String(rs.getRecord(id));
    item++;
    append(str2);------->i have a problem here, i am unable to place in an array
    //System.out.println("Values which start with" + str2);//+" are "+str1[i]);
    //searchResult+=str2;
    //System.out.println("Search Results: "+searchResult);

    }


    }

    re.destroy(); // Free enumerator
    }
    itemDetails=items.toString();
    System.out.println("Items are: "+items.toString());
    }
    catch (Exception e)
    {
    System.out.println("Exception in searching for a record: "+e.getMessage());
    }
    }

    /*--------------------------------------------------
    * Search for text within a record
    * Each record passed in contains only text (String)
    *-------------------------------------------------*/
    public class SearchFilter implements RecordFilter
    {
    private String searchText = null;
    private String searchNoResult = null;

    public SearchFilter(String searchText)
    {
    // This is the text to search for
    this.searchText = searchText.toLowerCase();
    }

    public boolean matches(byte[] candidate)
    {
    String str = new String(candidate).toLowerCase();

    // Look for a match
    if (searchText != null && str.startsWith(searchText))
    return true;
    else
    searchNoResult="No Item Found";
    return false;
    }
    }

    Please help me out


    Dear udaykapavarapu welcome to the forum nokia. Are you getting any exception or there is any logical error in storing data ?

  4. #4
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: RMS record search and to place in an array

    Hi,
    Would appreciate if we all can try not to quote the complete section of the post..

    if (re.numRecords() > 0)
    {
    while (re.hasNextElement()) {
    int id = re.nextRecordId();
    System.out.println("Record ID: " + id);
    String str2 = null;
    str2 = new String(rs.getRecord(id));

    item++;
    append(str2);------->i have a problem here, i am unable to place in an array
    //System.out.println("Values which start with" + str2);//+" are "+str1[i]);
    //searchResult+=str2;
    //System.out.println("Search Results: "+searchResult);

    }
    }
    Can you please provide the more details..about the issue you are facing...
    which exception/error you are getting?
    is this null pointer exception..

    One thing I would like to tell you is that you are declaring the string in a while loop as locally and then assigning the value....why do not you declare that string out the loop......try this out.

    else come with more details...
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  5. #5
    Super Contributor
    Join Date
    Sep 2008
    Location
    Noida, U.P.
    Posts
    1,330

    Thumbs up Re: RMS record search and to place in an array

    hi,
    why r u making ur code so confusing. U have to just read out the records from RMS and have to save them in a string array..

    can u try this..

    int number = 0;
    String[] records;
    try{
    openDB();
    number = recordSTORE.getNumRecords();
    System.out.println("NUMBER OF RECORDS ---"+number);
    closeDB();
    }catch(Exception exception)
    {
    exception.printStackTrace();
    }
    records = new String[number];
    try{
    openDB();
    for (int i=0; i<number;i++)
    {
    records[i] = new String(recordSTORE.getRecord(i+1));
    }
    closeDB();
    }
    catch(Exception exception)
    {
    exception.printStackTrace();
    }

    If ur need is quite different then please do post with full explanations and problems..
    thanks,
    jitu_goldie..

    KEEP TRYING..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×