J2ME Record deletion problem in RecordStore - java-me

I am using a recordstore to store some data,and each data is being shown in form of list elements.In my application,i am having a feature in which i can delete a particular list element.When i perform this action,item is getting deleted from the list but it is not working with the record.
I dont know why?
Can any one help me?
My code snippet is:
int x=list.getSelectedIndex();
list.delete(x);
try
{
rs_store.deleteRecord(x);
}
catch(Exception error)
{
System.out.print("error");
}
display.setCurrent(list);
Thanks in advance

If the indices of the items in your List are supposed to correspond with the indices of items in your RecordStore, then your problem may be down to the fact that RecordStore entries are 1-based, not 0-based.
So element 0 in your list will correspond to record 1 etc.

Related

how to skip unique constraint?

I have tried to insert many records into a table, and this table has a unique constraint, so when if one user try to add a new record with the same unique value, I get the dbUpdateException.
But I would like to know how to skipt this error and try to add the remaining records that the first user are trying to add to the table.
How can do that?
Thanks.
One approach could be to catch the DbUpdateException, and use its Entries property to remove the duplicate entities from the context.
You could then retry the save - rinse and repeat - and eventually all the non-duplicate entities will be saved.
E.g.
var duplicates = new List<MyEntity>();
...
catch(DbUpdateException ex)
{
ex.Entries.Each(e => DbContext.Entry(e).State = EntityState.Detached;
duplicates.Add(ex.Entries);
ReTrySave(); // do whatever you need todo to re-enter your saving code
}
...
// Report to user the duplicate entities
ReportToUser(duplicates);
NOTE - treat as pseudo code as I haven't attempted to compile this snippet.

How to restrict rows in a list inside a document?

I have a document something like:
{"name":"Stock levels",
"content":[
{"sku":"328143",
"name":"Battery",
"stocklevel":"100",
"warehouse":"london"},
{"sku":"328143",
"name":"Battery",
"stocklevel":"20",
"warehouse":"manchester"},
{"sku":"328143",
"name":"Battery",
"stocklevel":"30",
"warehouse":"brighton"}]}
Where the list "content" could have quite a lot of rows.
What I want to do is return an internal row count and just one row from the list.
e.g.
{"name":"Stock levels",
"rows" : "2300",
"content":[
{"sku":"328143",
"name":"Battery",
"stocklevel":"100",
"warehouse":"london"}]}
How might I achieve this in CouchDb? My initial thought is using a list to effectively rebuild the document and inserting the extra rows field and restricting the number of rows return internally, but I am not sure if this is the best approach.
Thanks
You can use a view,
the following example allow you to search based on document id
(which is emit as key)
function(doc)
{
if (doc._id == "xxx")
{
emit(doc._id, {name:doc.name, rows:doc.content.length, content:doc.content[0]});
}
}

How to set record id by myself in J2ME

I use RecordStore to store my data.
I know when we store data in RecordStore, it automatically generates a record id for each record.
But how can I set the record id by myself? Or how can I get the record id?
Because I want to use the recordstore.setRecord(..) method to update my recordstore.
But when I use RecordEnumeration to fetch RecordStore and use method nextRecordId(), it just shows odd or even ids. I mean when I have 8 records, it just prints out only odd or even records like
2 4 6 8
My code:
handleRecord.openRecordStore(handleRecord.getRecordName());
RecordEnumeration re;
try {
int rc = handleRecord.getRecordStore().getNumRecords();
re = hrs.getRcs().enumerateRecords(null, null, true);
while(re.hasNextElement()) {
int rid = re.nextRecordId();
System.out.println(rid);
}
} catch(Exception e) {
System.out.println(e.toString());
}
MIDP API doesn't have method to set record id by yourself.
See RecordStore API documentation for explanation how this is supposed to work.
"Records are uniquely identified within a given record store by their recordId, which is an integer value. This recordId is used as the primary key for the records. The first record created in a record store will have recordId equal to one (1). Each subsequent record added to a RecordStore will be assigned a recordId one greater than the record added before it. That is, if two records are added to a record store, and the first has a recordId of 'n', the next will have a recordId of 'n + 1'..."
The code that iterates the store appears OK:
re = hrs.getRcs().enumerateRecords(null, null, true);
while(re.hasNextElement()) {
int rid = re.nextRecordId();
System.out.println(rid);
}
if you're getting only odd or even record like 2-4-6... or 1-3-5... printed as a result, first thing to check is whether you somehow removed records that are "missing" - this could be done eg using RecordStore.getVersion method:
"Each time a record store is modified (by addRecord, setRecord, or deleteRecord methods) its version is incremented. This can be used by MIDlets to quickly tell if anything has been modified..."

Find by String in a GWT ListBox

I would like to find the index of an item inside a GWT Listbox by specifying a String value.
For example, if I had a GWT ListBox comprising the following items: "Randy", "Bob" and "Helen", the method i'm looking to implement would return the value 1 if I called it with parameter "Bob".
From what i'm seeing in the ListBox javadoc, ther does not seem to be any quick method to do this.
Any ideas?
Thanks in advance!
My idea of implementation as TextBox doesn't provide this out of the box. Store all the items in a list and in the order you want them to be part of ListBox.
List<String> orderedItems=new ArrayList<String>
orderedItems.add(0,"Randy");
orderedItems.add(1,"Bob");
orderedItems.add(2,"Helen");
//adding items in the same order as they are in List is the key
for(String item:items)
{
lb.addItem(item);
}
then you can find the index using List's indexOf(..) method

Sharepoint event handling.. which column changed?

I'm writing an event handler to handle the updating of a particular SPItem in a list. The event is asynchronous and I get the SPEvenItemProperties without a problem. What I would like to find out is which of the SPItems columns' actually fired the event. Anyone have any idea how?
Thanks in advance.
Your answer depends a bit on from where and how the SPListItem is retrieved. In a regular list, you do not have access to the previous values of the item. If you turn on versioning, you can get access to the previous versions, depending on permissions, of course.
For a document library you can use the SPItemEventProperties.BeforeProperties to get the previous metadata for a document.
For a document library you can try something like this:
foreach (DictionaryEntry key in properties.BeforeProperties)
{
string beforeKey = (string)key.Key;
string beforeValue = key.Value.ToString();
string afterValue = "";
if (properties.AfterProperties[beforeKey] != null)
{
afterValue = properties.AfterProperties[beforeKey].ToString();
if (afterValue != beforeValue)
{
// Changed...
}
}
}
.b
I think the best way to do this would be to look through the BeforeProperties and AfterProperties of the SPItemEventProperties and check which fields have different values.
These contain the values of all fields of the item before and after the event occurred.

Resources