What is the difference between `id` and `pos` n a Spinner's OnItemSelectedListener? - android-spinner

When creating a Spinner, I assign an OnItemSelectedListener using setOnItemSelectedListener. When a select an item in the spinner, the onItemSelected method is called, with arguments including position and id.
From my observations, position and id always have the same values. Is there any difference in the meaning of these values when using an OnItemSelectedListener with a Spinner?

Related

Why default lines for One2many fields are not filling in related and default values automatically in Odoo 13?

I have a button in the model sale.order with executes the method action_open_certification_views. This method opens the sale.certification tree view. So the method returns the dictionary of the action which opens the sale.certification views, and I have added the following context to auto-fill in the sale.certification fields (sale_id and line_ids, where line_ids is a One2many field pointing to sale.certification.line):
def action_open_certification_views(self):
...
action['context'] = {
'default_name': 'My Certification',
'default_sale_id': self.id,
'default_line_ids': [
(0, 0, {
'quantity': 5.0,
'sale_line_id': line.id,
}) for line in self.order_line
],
}
return action
When I click on the button, the sale.certification tree view is opened right, and if click on Create button, the context of the action is working well since I see the sale.certification form filled in automatically with the default name My Certification, the right link to sale order, and the same number of certification lines as sale order lines has the sale order. Each of this certification lines has quantity 5.0, and the link to sale order line is right too.
The problem is that the sale.certification.line model has many other fields which are related or which have default values, but these certification lines filled in by default by the action context are not filling in those fields.
For example, there is another field quantity2 in sale.certification.line model, and visible in the view, which always takes by default 1.0. However, the field is empty in all default lines. Same problem with related fields. There are some related fields in the view which take their values through sale_line_id field. These one is filled in OK, but the related fields remain empty. For example, in `sale.certification.line model there is this field:
product_uom = fields.Many2one(
related='sale_line_id.product_uom',
)
In spite of having sale_line_id filled in OK in all lines, they have product_uom empty. Can anyone tell me why? Do I have to specify their values in context too, despite they should have been filled in by theirselves?

Web2Py list:reference table, Load and set data

Maybe I'm missing something absurd, I'm not seeing, but this is my first app to study web2py.
I am unable to enter the data in Table Movies, which has fields related to other tables.
The list is loaded, but it is not registered in Movies registration.
Under the codes and the results.
db.py
Movie = db.define_table('movies',
Field('title','string', label = 'Title'),
Field('date_release','integer', label = 'Date Release'),
Field('duraction','integer', label = 'Duraction'),
Field('category','string','list:reference categories', label = 'Category'),
Field('actor','list:reference actors', label = 'Actor'),
Field('director','list:reference directors', label = 'Diretor'),
)
Category = db.define_table('categories',
Field('title','string', label = 'Title'),
)
validators.py
Movie.title.requires = [IS_NOT_EMPTY(), IS_NOT_IN_DB(db, 'movies.title')]
Movie.category.requires = IS_IN_DB(db, 'categories.title')
Movie.director.requires = IS_IN_DB(db, 'directors.name')
Movie.actor.requires = IS_IN_DB(db, 'actors.name')
Movie.duraction.requires = IS_INT_IN_RANGE(0, 1000)
Category.title.requires = IS_NOT_EMPTY()
movie.py
def add():
form = SQLFORM(Movie)
if form.process().accepted:
response.flash = "Successful! New movie added!"
redirect(URL('add'))
elif form.errors:
response.flash = 'Error'
else:
response.flash = 'Form, set data'
return dict(form = form)
List Load another tables - ok:
The items of list not record in DB:
The widgets displayed in the form are based on the IS_IN_DB field validators you have specified, and there are three problems with the way you have coded them.
First, list:reference fields, like standard reference type fields, store the record IDs of the records they reference -- they do not store values of other fields within the referenced records. So, the second argument to the IS_IN_DB validator should always be the ID field (e.g., categories.id).
Second, although the field will store record IDs, you want the form widget to show some other more descriptive representation of each record, so you should specify the "label" argument of the IS_IN_DB validator (e.g., label='%(title)s').
Third, list:reference fields allow for multiple selections, so you must set the "multiple" argument of the IS_IN_DB validator to True. This will result in a multi-select widget in the form.
So, the resulting validator should look like this:
Movie.category.requires = IS_IN_DB(db, 'categories.id', label='%(title)s', multiple=True)
The above will allow multiple db.categories IDs to be selected, though the form widget will display category titles rather than the actual IDs.
Now, all of the above can be made much easier if you instead define the referenced tables before the db.movies table and specify a format argument for each table:
Category = db.define_table('categories',
Field('title','string', label = 'Title'),
format='%(title)s')
Movie = db.define_table('movies',
...,
Field('category', 'list:reference categories', label = 'Category'),
...)
With the above code, there is no need to explicitly specify the IS_IN_DB validator at all, as the db.movies.category field will automatically get a default validator exactly like the one specified above (the format attribute of the db.categories table is used as the label argument).
You might want to read the documentation on list:reference fields and the IS_IN_DB validator.
As an aside, you might consider specifying your field validators within the table definitions (via the requires argument to Field()), as this is more concise, keeps all schema-related details in one place, and eliminates the need to read and execute an additional model file on every request.

Converting nested attributes from string to numeric

I have a GALLERY model containing nested SLIDE attributes. Some of my attributes are enum values. Without intervention my params hash receives the enum values as "0" or "1". This results in an error e.g. '0' is not a valid gallery type.
Here is an example of my params hash:
{"name"=>"Video quis terrax", "gallery_type"=>"0",
"slides_attributes"=>{"0"=>{"id"=>"2", "order"=>"0",
"slide_type"=>"0", "image_cache"=>"", "embedded_url"=>"",
"_destroy"=>"0"},
"1441761800650"=>{"order"=>"0", "slide_type"=>"0",
"image"=>#,
#original_filename="large_veh_photo.jpg",
#content_type="image/jpeg", #headers="Content-Disposition: form-data;
name=\"gallery[slides_attributes][1441761800650][image]\";
filename=\"large_veh2.jpg\"\r\nContent-Type: image/jpeg\r\n">,
"image_cache"=>"", "embedded_url"=>"", "thumb_text"=>"2nd Image",
"_destroy"=>"0"},
"1441761824429"=>{"order"=>"0", "slide_type"=>"0",
"image_cache"=>"", "embedded_url"=>"", "_destroy"=>"0"}}}
You will notice that there are 3 slides in this example. The first one is identified by "0", the 2nd one by "1441761800650" and the 3rd one by "1441761824429". The first slide has already been saved. The 2nd one is a new slide and the 3rd one has no details filled in. These long number are automatically allocated by Rails in the view.
How can I change the "order" and "slide_type" attributes to numeric values before the controller attempts to do
#gallery.update(gallery_params)
Do I have any control over the random numbers assigned to my slides in the view?
You need to clean up the params in the controller. You should be able to do something like this:
Class SomeController < ActionController
before_filter: clean_gallery_params
def clean_gallery_params
params[:slides_attributes].each do |slide_attribute|
params[:slides_attributes][slide_attribute][:order] = params[:slides_attributes][slide_attribute][:order].to_i
params[:slides_attributes][slide_attribute][:slide_type] = params[:slides_attributes][slide_attribute][:slide_type].to_i
end
end
...

Binding an edit box within a custom control to a form field programatically

I have a notes form with a series of fields such as city_1, city_2, city_3 etc.
I have an XPage and on that XPage I have a repeat.
The repeat is based on an array with ten values 1 - 10
var repArray = new Array() ;
for (var i=1;i<=10;i++) {
repArray.push(i) ;
}
return(repArray) ;
Within the repeat I have a custom control which is used to surface the fields city_1 through city_10
The repeat has a custom property docdatasource which is passed in
It also has a string custom property called cityFieldName which is computed using the repeat
collection name so that in the first repeat row it is city_1 and in the second it is city_2 etc..
The editable text field on the custom control is bound using the EL formula
compositeData.docdatasource[compositeData.cityFieldName]
This works fine but each time I add new fields I have to remember to create a new custom property and then a reference to it on the parent page.
I would like to be able to simply compute the data binding such as
compositeData.docdatasource['city_' + indexvar]
where indexvar is a variable representing the current row number.
Is this possible ? I have read that you cannot use '+' in Expression Language.
First: you wouldn't need an array for a counter. Just 10 would do (the number) - repeats 10 times too. But you could build an array of arrays:
var repArray = [];
for (var i=1;i<=10;i++) {
repArray.push(["city","street","zip","country","planet"]) ;
}
return repArray;
then you should be able to use
#{datasource.indexvar[0]}
to bind city,
#{datasource.indexvar[1]}
to bind street. etc.
Carries a little the danger of messing with the sequence of the array, if that's a concern you would need to dig deeper in using an Object here.
compute to javascript and use something like
var viewnam = "#{" + (compositeData.searchVar )+ "}"
return viewnam
make sure this is computed on page load in the custom control
I was never able to do the addition within EL but I have been very successful with simply computing the field names outside the custom control and then passing those values into the custom control.
I can send you some working code if you wish from a presentation I gave.

How to know programmatically the actual modified component?

In J2ME there is the interface ItemStateListener which can detect exactly the item which is being changed. Is there a similar way in LWUIT ? I tried using the DataChangedListener interface but it gives the character position within the TextField , or -1 , for the index argument ! So ....
Initially set the boolean value to false and add the listener for the components then if any event occurs on that components, set the boolean value to true. Use the addDataChangeListener(It only for TextField) or addActionListener for the components. On addDataChangeListener returns two index values. Initially returns -1 and then returns current text position of the TextField. AFAIK On TextField, they set the default index value is -1 for fireDataChanged on setText method. That is why it will returns -1 initially.

Resources