<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
  <META NAME="GENERATOR" CONTENT="GtkHTML/4.2.2">
</HEAD>
<BODY>
Dear All,<BR>
I'm converting a plugin with new API but I still doen't figure out how to add attribute to feature.<BR>
I know it's a stupid question but i think the cookbook is till not updated?<BR>
 <A HREF="http://qgis.org/it/docs/pyqgis_developer_cookbook/vector.html#add-features">http://qgis.org/it/docs/pyqgis_developer_cookbook/vector.html#add-features</A><BR>
<BR>
I tried this code without success:<BR>
<BR>
vl = QgsVectorLayer("Point?crs=epsg:4326", 'test', "memory")<BR>
pr = vl.dataProvider()                 <BR>
#~ add fields<BR>
pr.addAttributes( [QgsField('text', QVariant.String),QgsField('date', QVariant.String)])
fields = vl.pendingFields()
feature = QgsFeature(fields)
feature[0] = 'some text'
Traceback (most recent call last):<BR>
  File "<input>", line 1, in <module><BR>
KeyError: '0'<BR>
<BR>
<BR>
Below I attached the code I used in 1.8 <BR>
<BR>
<BR>
vl = QgsVectorLayer("Point?crs=epsg:4326", 'test', "memory")<BR>
pr = vl.dataProvider()                        <BR>
 #~ add fields<BR>
 pr.addAttributes( [QgsField('text', QVariant.String),QgsField('date', QVariant.String)])                           <BR>
#~ add features point<BR>
for pointID in range(len(coordNotes)):<BR>
       fet = QgsFeature()<BR>
        xcoor=float(coordNotes[pointID][0])<BR>
        ycoor=float(coordNotes[pointID][1])<BR>
        fet.setGeometry( QgsGeometry.fromPoint(QgsPoint(xcoor,ycoor))) <BR>
        #~ add attribute value<BR>
         fet.addAttribute(0, QVariant( dateNote[pointID][0] ))<BR>
         fet.addAttribute(1, QVariant( dateNote[pointID][1] ))<BR>
pr.addFeatures( [ fet ] )<BR>
#~ update vector layer    <BR>
vl.updateExtents()<BR>
vl.updateFieldMap()<BR>
<BR>
<BR>
Thanks<BR>
Pierluigi<BR>
<TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
-- <BR>
<BR>
<H4>
<TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<DIV ALIGN=center>
<HR ALIGN="center">
</DIV>
<H6>
<B><FONT SIZE="1"><FONT COLOR="#993300">Ing. Pierluigi De Rosa (PhD)</FONT></FONT></B><BR>
<B><FONT SIZE="1"><FONT COLOR="#993300">Studio Associato GFOSSERVICES</FONT></FONT></B>
</H6>
                <IMG SRC="cid:1381133837.4950.17.camel@pierluigi-Aspire-6930G" WIDTH="53" ALIGN="bottom" BORDER="0"><BR>
<B><FONT SIZE="1"><FONT COLOR="#993300">Via Tilli 58 - 06127 Perugia (PG)</FONT></FONT></B> 
<H6>
<B><FONT SIZE="1"><FONT COLOR="#993300">tel: 075 7825101 / fax: 075 7823038</FONT></FONT></B><BR>
<B><FONT SIZE="1"><FONT COLOR="#993300">cel: 3497558268</FONT></FONT></B><BR>
<B><FONT SIZE="1"><FONT COLOR="#993300">web</FONT></FONT></B><B><FONT SIZE="1">: <A HREF="http://www.gfosservices.com/index.php?option=com_content&view=article&id=21&Itemid=21&lang=it">www.gfosservices.it</A></FONT></B><BR>
<B><FONT SIZE="1"><FONT COLOR="#993300">skype: pierluigi.derosa </FONT></FONT></B>
</H6>
</TD>
</TR>
</TABLE>
</H4>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>