I have a database where users will be taking pictures and adding them to local db. They're not using the default uIS picture gallery, so I created my own, basically looping through the database and creating everything and adding it to the scroll view.
There is also a "list" button that opens a standard tableview with the same information.
When I delete from the tableview, they're deleted from the database. However, when they go back (this is opened in a tab window), they're still there in the scrollview.
Anyone have an idea? I was thinking about putting it in a setInterval, but I'm not sure how to set that up with this.
var win = Titanium.UI.currentWindow; var db = Titanium.Database.install('pictonote.sqlite','pictonote.sqlite'); var listButton=Titanium.UI.createButton({ title:'List' }); listButton.addEventListener('click',function() { var listWindow=Titanium.UI.createWindow({ title:'List', url:'galleryList.js' }); Ti.UI.currentTab.open(listWindow); }); win.setRightNavButton(listButton); setInterval (refreshData, 2500); function refreshData() { var scroller=Titanium.UI.createScrollView({ contentWidth:'auto', contentHeight:'auto', top:0, showVerticalScrollIndicator:true, showHorizontalScrollIndicator:true }); var i=0; var top=0; var rowcount=1; var sql=("SELECT * from pictures"); /***database part***/ var data = []; //Get data for tableview var rows = db.execute(sql); while (rows.isValidRow()) { data.push({ id: rows.fieldByName('id'), date_taken: rows.fieldByName('date_taken'), path: rows.fieldByName('path') }); rows.next(); } rows.close(); /**********/ while (i<data.length) { var images=Titanium.UI.createImageView({ id:data[i].id, top:top, image:Titanium.Filesystem.applicationDataDirectory + data[i].path, width:'100', height:'100' }); Ti.API.info('rowcount:' +rowcount); if(rowcount==1) { images.left=5; rowcount++; } else if(rowcount==2) { rowcount++; } else if(rowcount==3) { images.right=5; rowcount=1; top=top+120; } scroller.add(images); i++; //here is what happens when you click the image images.addEventListener('click', function(e) { //alert(e.source.id); }); //end image click } //end while win.add(scroller);
4 Answers
Accepted Answer
you want to fire an event when the item is deleted and then listen for the event and refresh the view from the database
Do something like:
while (i<data.length) { var images=Titanium.UI.createImageView({ id:data[i].id, top:top, image:Titanium.Filesystem.applicationDataDirectory + data[i].path, width:'100', height:'100' }); Ti.API.info('rowcount:' +rowcount); if(rowcount==1) { images.left=5; rowcount++; } else if(rowcount==2) { rowcount++; } else if(rowcount==3) { images.right=5; rowcount=1; top=top+120; } scroller.add(images); i++; //here is what happens when you click the image images.addEventListener('click', function(e) { //alert(e.source.id); }); //end image click // custom eventlistener to delete view Ti.addEventListener('event_name'+i, function() { images.hide(); }); } Ti.fireEvent('event_name'+i);Pass variable i with click event and use this to fire event.
Yeah this solution is not suited for more than 30 images in this view. Go with #Aaron in that case.
Got it working finally, after figuring out:
pageThatNeedsRefresh.js
Add the event listener with the code in itpageDoingSomething.js
call the event listener
Your Answer
Think you can help? Login to answer this question!