android - Extracting Sms in an xml file -
//checksms returning list sms private arraylist<sms> checksms() { uri urismsuri = uri.parse("content://sms/inbox"); cursor cur = getactivity().getcontentresolver().query(urismsuri, null, null, null, null); while (cur.movetonext()) { newsms.setsender(cur.getstring(cur.getcolumnindex("address"))); mylist.add(newsms); } createxml(); return (mylist); }
//here createxml methode
private void createxml() { file newxmlfile = new file(environment.getexternalstoragedirectory()+ "/smsfile.xml"); try {log.v(backupfragment.this.getclass().getname(), "create file:" + newxmlfile.createnewfile());} catch (ioexception e) {log.e("ioexception", "exception in createnewfile() method");} fileoutputstream fileos = null; try {fileos = new fileoutputstream(newxmlfile);} catch (filenotfoundexception e) {log.e("filenotfoundexception", "can't create fileoutputstream");} xmlserializer serializer = xml.newserializer(); try { serializer.setoutput(fileos, "utf-8"); serializer.startdocument(null, boolean.valueof(true)); serializer.setfeature("http://xmlpull.org/v1/doc/features.html#indent-output",true); serializer.starttag("", "document"); for(int = 0 ; < mylist.size(); i++) { serializer.starttag("", "sms"); serializer.starttag(null, "sender"); serializer.text(newsms.getsender()); serializer.endtag(null, "sender"); serializer.endtag("", "sms"); } serializer.endtag("", "document"); serializer.enddocument(); serializer.flush(); fileos.close(); } catch (exception e) { log.e("exception", "error occurred while creating xml file"); } }
the result wrong
it's not returning right result want have it's repeating same wrong number " 44225627565 " , don't know te problem
you want iterate on mylist
, doing here:
for(int = 0 ; < mylist.size(); i++) { serializer.starttag("", "sms"); serializer.starttag(null, "sender"); serializer.text(newsms.getsender()); <---- serializer.endtag(null, "sender"); serializer.endtag("", "sms"); }
but using newsms.getsender()
receive number. responsible getting same number.
instead should use: mylist.get(i).getsender()
or whatever mylist includes.
edit
insert same object list:
newsms.setsender(cur.getstring(cur.getcolumnindex("address"))); mylist.add(newsms);
you should create new object inserting:
sms tmp = new sms(); tmp.setsender(cur.getstring(cur.getcolumnindex("address"))); mylist.add(tmp);
Comments
Post a Comment