im using for first time appcelerator for a Iphone app, im using a TableViewRow, when the window load all is fine, but if i want to refresh the window, the TableViewRow repeat the same data every time that i push refresh, so, i have 5 Categories, after refresh im having the same Categories repeting every time that im trying to refresh.
Ti.UI.backgroundColor = '#dddddd';
var url = "http://remoteJSON.js";
var win = Titanium.UI.currentWindow;
var table = Titanium.UI.createTableView();
var tableData = [];
var json, categorias, categoria, i, row, categoriaLabel, descripcionLabel;
var refreshBtn = Titanium.UI.createButton({
systemButton: Ti.UI.iPhone.SystemButton.REFRESH
});
refreshBtn.addEventListener('click', function() {
loadTutoriales();
});
win.setRightNavButton(refreshBtn);
var actInd = Titanium.UI.createActivityIndicator({
bottom:10,
height:50,
width:10,
style:Titanium.UI.iPhone.ActivityIndicatorStyle.PLAIN
});
function loadTutoriales(){
if(!Titanium.Network.online){
alert("Debe conectarse a internet.");
}
var xhr = Ti.Network.createHTTPClient({
onload: function() {
actInd.show();
actInd.message = 'Loading...';
//Ti.API.debug(this.responseText);
json = JSON.parse(this.responseText);
for (i = 0; i < json.categorias.length; i++) {
var rowColor = '#eeeeee';
if(i & 1){
rowColor = '#ffffff';
}
categoria = json.categorias[i];
var row = Ti.UI.createTableViewRow({
height:'auto',
hasChild:true
});
row.backgroundColor=rowColor;
/* add */
var post_view = Titanium.UI.createView({
height:'auto',
layout:'vertical',
top:5,
right:5,
bottom:5,
left:5
});
var av_image = Titanium.UI.createImageView({
image:categoria.imageUrl,
top:0,
left:0,
height:48,
width:48
});
post_view.add(av_image);
var inner_view = Titanium.UI.createView({
height:'auto',
layout:'vertical',
top:0,
right:0,
bottom:0,
left:0
});
/* end add */
var categoriaLabel = Ti.UI.createLabel({
text:categoria.categoryName,
left:54,
width:120,
top:-48,
bottom:0,
height:18,
textAlign:'left',
color:'#444444',
font:{fontFamily:'Trebuchet MS',fontSize:14,fontWeight:'bold'}
});
inner_view.add(categoriaLabel);
var descripcionLabel = Ti.UI.createLabel({
text:'"' + categoria.description + '"',
left:54,
top:0,
bottom:2,
height:'auto',
width:'auto',
textAlign:'left',
font:{fontSize:14}
});
inner_view.add(descripcionLabel);
post_view.add(inner_view);
row.add(post_view);
//row.add(descripcionLabel);
tableData.push(row);
}
table.setData(tableData);
},
onerror: function(e) {
Ti.API.debug("STATUS: " + this.status);
Ti.API.debug("TEXT: " + this.responseText);
Ti.API.debug("ERROR: " + e.error);
alert('There was an error retrieving the remote data. Try again.');
},
timeout:5000
});
//win.add(actInd);
win.add(table);
win.open();
//actInd.hide();
xhr.open("GET", url);
xhr.send();
}
loadTutoriales();
When you press the refresh button clear the tableData array and assign to table set data function before you fill your table again. So it will empty your table first and than fill new refreshed data.
Like this,
refreshBtn.addEventListener('click', function() {
tableData = [];
table.setData(tableData);
loadTutoriales();
});
Related
I am using arcgis js api.I have called all necessary esri class modules and all class name in order. I am using query task to add points to map as a graphic layer.I am getting error for graphic.I am not able to trace the error.
var map;
var toc;
var mapserviceurl = "http://......./arcgisserver/rest/services/CRD/CRD1/MapServer";
require(["dojo/parser",
"dojo/on", "esri/map","esri/dijit/HomeButton",
"esri/dijit/Measurement", "dojo/_base/lang","esri/layers/ArcGISDynamicMapServiceLayer",
"esri/layers/FeatureLayer","esri/graphic","esri/layers/GraphicsLayer",
"esri/dijit/Scalebar","esri/dijit/BasemapGallery","esri/toolbars/navigation",
"esri/dijit/OverviewMap","esri/geometry/Extent","esri/SpatialReference",
"esri/geometry/webMercatorUtils","esri/tasks/query","esri/tasks/QueryTask",
"esri/toolbars/draw","esri/symbols/SimpleLineSymbol","esri/symbols/SimpleFillSymbol",
"esri/symbols/PictureMarkerSymbol","esri/symbols/SimpleMarkerSymbol",
"esri/renderers/SimpleRenderer","esri/geometry/Point","esri/Color",
"agsjs/dijit/TOC","esri/InfoTemplate", "esri/tasks/IdentifyTask",
"esri/tasks/IdentifyParameters", "esri/dijit/Popup", "dojo/dom-construct",
"esri/tasks/locator","dojo/_base/array", "dojo/domReady!"],
function (parser, on, Map, HomeButton, Measurement,lang, ArcGISDynamicMapServiceLayer, FeatureLayer, graphic, GraphicsLayer, Scalebar, BasemapGallery, Navigation, OverviewMap, Extent, SpatialReference,
webMercatorUtils, Query, QueryTask, DrawToolbar, SimpleLineSymbol, SimpleFillSymbol,
PictureMarkerSymbol, SimpleMarkerSymbol, SimpleRenderer,Point,Color,TOC,InfoTemplate,IdentifyTask,IdentifyParameters,
Popup,domConstruct,Locator,arrayUtils) {
parser.parse();
createDialogs();
var identifyTask, identifyParams;
var intialextent = new Extent(7778597.959975056, 1564947.1810059766, 9217107.340624947, 2133123.2518000016, new SpatialReference({ wkid: 102100 }));
map = new Map("divMap", {
basemap: "streets",
center: [75, 14],
zoom: 7,
extent: intialextent,
logo: false,
fitExtent: true,
slider: true
});
var operationalLayer = new ArcGISDynamicMapServiceLayer(mapserviceurl);
var samplelocations = new GraphicsLayer({ id: "Samplelocations" });
var samplelocationsSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, 10, SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([239, 107, 0]), 1), new Color([239, 107, 0]));
var samplelocationrenderer = new SimpleRenderer(samplelocations);
samplelocations.setRenderer(samplelocationrenderer);
var home = new HomeButton({
map: map
}, "HomeButton");
home.startup();
map.on('layers-add-result', function (evt) {
try {
var toc = new TOC({
map: map,
layerInfos: [{
layer: pointFeatureLayer,
title: "My Feature"
}, {
layer: operationalLayer,
title: "Dynamic Map"
}]
}, "tocDiv");
toc.startup();
toc.on("load", function () {
console.log("TOC loaded");
});
}
catch (e) {
console.error(e.message);
}
});
map.addLayers([operationalLayer, pointFeatureLayer, samplelocations]);
var basemapGallery = new BasemapGallery({
showArcGISBasemaps: true,
map: map
}, "basemapGallery");
basemapGallery.on("load", function () {
basemapGallery.remove('basemap_1');
basemapGallery.remove('basemap_2');
basemapGallery.remove('basemap_3');
basemapGallery.remove('basemap_4');
basemapGallery.remove('basemap_5');
basemapGallery.remove('basemap_8');
});
basemapGallery.startup();
var Scalebar = new Scalebar({
map: map,
scalebarUnit: 'metric',
scalebarStyle: 'line'
});
var measurement = new Measurement({
map: map
}, measurementDiv);
measurement.startup();
map.on("mouse-move", showcoordiantes);
map.on("mouse-drag", showcoordiantes);
//overview tools
var OverviewMap = new OverviewMap({
map: map,
attachTo: "bottom-right",
color: " #D84E13",
opacity: .40
});
OverviewMap.startup();
function showcoordiantes(evt) {
var p = webMercatorUtils.webMercatorToGeographic(evt.mapPoint);
$("#latlong").html("Lat,Long : " + p.y.toFixed(4) + "," + p.x.toFixed(4));
}
$("#ClearGraphics").click(function (e) {
e.preventDefault();
// drawtools.deactivate();
map.infoWindow.hide();
map.setMapCursor("default");
map.graphics.clear();
});
var navToolbar = new Navigation(map);
//zoom In
$("#ZoomInTool").click(function (e) {
e.preventDefault();
map.setMapCursor("url('images/cursors/zoomin.cur'), auto");
navToolbar.activate(Navigation.ZOOM_IN);
});
//ZoomOut
$("#ZoomOutTool").click(function (e) {
e.preventDefault();
map.setMapCursor("url('images/cursors/zoomout.cur'), auto");
navToolbar.activate(Navigation.ZOOM_OUT);
});
//Pan
$("#panTool").click(function (e) {
e.preventDefault();
map.setMapCursor("url('images/cursors/pan.cur'), auto");
navToolbar.activate(Navigation.PAN);
});
//FullExtent
$("#zoomfullext").click(function (e) {
e.preventDefault();
map.setMapCursor("default");
navToolbar.deactivate();
// navToolbar.zoomToFullExtent();
map.setExtent(intialextent, true);
});
//Zoom to previous
$("#zoomtoPrevExtent").click(function (e) {
e.preventDefault();
map.setMapCursor("default");
navToolbar.deactivate();
navToolbar.zoomToPrevExtent();
});
//Zoom to Next
$("#zoomtoNextExtent").click(function (e) {
e.preventDefault();
map.setMapCursor("default");
navToolbar.deactivate();
navToolbar.zoomToNextExtent();
});
map.on("load", showresults)
function showresults() {
var queryTask = new esri.tasks.QueryTask('http://......./arcgisserver/rest/services/CRD/CRD2/MapServer/0');
var query = new esri.tasks.Query();
symbol = new esri.symbol.SimpleMarkerSymbol();
symbol.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE);
symbol.setSize(10);
symbol.setColor(new dojo.Color([255, 255, 0, 0.5]));
query.returnGeometry = true;
query.outFields = ["*"];
query.outSpatialReference = map.spatialReference;
query.where = "objectid = '" + 5281902 + "'";
map.graphics.clear();
queryTask.execute(query, addPointsToMap);
function addPointsToMap(featureSet) {
var graphic = featureSet.features;
graphic.setSymbol(symbol);
map.graphics.add(graphic);
var extent = esri.graphicsExtent(features);
if (extent) {
map.setExtent(extent)
}
}
}
});
I am getting error as
You are assigning an array to the graphic variable:
var graphic = featureSet.features;
Select a single feature of the array and set the symbol then e.g.
var graphic = featureSet.features[0];
I have a json data in my remote server,and i want to show it in table view,here is my json data
[{"offerType":"Discount","offerName":"BURBERRY - Buy 2 get 1 free on men's leather coat collectioN"},{"offerType":"Discount","offerName":"ARMANI JUNIOR- 20% off on Purchase worth INR 30000"}]
var url = "http://203.122.12.58:8080/api? username=superuser&password=superuser&action=invokeService&serviceName=offerAction&methodNa me=getMyOffers&customerId=CUS-12220&storeId=CMPNY-3376";
var win = Ti.UI.createWindow();
win = Ti.UI.currentWindow;
win.setBackgroundColor('gray');
//win.setBackgroundColor = 'white';
var table = Ti.UI.createTableView();
var tableData = [];
var json, fighters, fighter, i, row, nameLabel, nickLabel,offerType,offerName;
var xhr = Ti.Network.createHTTPClient({
onload: function() {
// Ti.API.debug(this.responseText);
json = JSON.parse(this.responseText);
for (i = 0; i < json.length; i++) {
console.log("json = %d",i);
offerType = json[i].offerType;
row = Ti.UI.createTableViewRow({
height:'60dp'
});
nameLabel = Ti.UI.createLabel({
text:offerType,
font:{
fontSize:'24dp',
fontWeight:'bold'
},
height:'auto',
left:'10dp',
top:'5dp',
color:'#000',
touchEnabled:false
});
offerName = json[i].offerName;
nickLabel = Ti.UI.createLabel({
text:'"' + fighter.nickname + '"',
font:{
fontSize:'16dp'
},
height:'auto',
left:'15dp',
bottom:'5dp',
color:'#000',
touchEnabled:false
});
row.add(nameLabel);
row.add(nickLabel);
tableData.push(row);
}
table.setData(tableData);
},
onerror: function(e) {
Ti.API.debug("STATUS: " + this.status);
Ti.API.debug("TEXT: " + this.responseText);
Ti.API.debug("ERROR: " + e.error);
alert('There was an error retrieving the remote data. Try again.');
},
timeout:5000
});
xhr.open("GET", url);
xhr.send();
how can i achieve my goal,Thanks in advance.
Akshay
Your code was pretty close. I did make a few changes so you'll have to compare them to see how to fix it. This example reads your service and displays the 2 entries in a table.
See the comments in the code detailing how it was changed.
//**** Not sure if this is an issue with formating on Stackoverflow or copy and paste, but the spaces in the URL are causing an issue.****
//var url = "http://203.122.12.58:8080/api? username=superuser&password=superuser&action=invokeService&serviceName=offerAction&methodNa me=getMyOffers&customerId=CUS-12220&storeId=CMPNY-3376";
var url = "http://203.122.12.58:8080/api?username=superuser&password=superuser&action=invokeService&serviceName=offerAction&methodName=getMyOffers&customerId=CUS-12220&storeId=CMPNY-3376";
var win = Ti.UI.createWindow();
//*****If you are calling this from another window, you may need this is your overall code, but my example I removed it.*****
//win = Ti.UI.currentWindow;
win.setBackgroundColor('gray');
//win.setBackgroundColor = 'white';
var table = Ti.UI.createTableView();
var tableData = [];
// **** Removed variable fighters because it is never used.
// **** Removed the fighter variable because it is never used.
var json, i, row, nameLabel, nickLabel, offerType, offerName;
var xhr = Ti.Network.createHTTPClient({
onload: function() {
// Ti.API.debug(this.responseText);
json = JSON.parse(this.responseText);
for (i = 0; i < json.length; i++) {
console.log("json = %d",i);
offerType = json[i].offerType;
row = Ti.UI.createTableViewRow({
height:'60dp'
});
nameLabel = Ti.UI.createLabel({
text: offerType,
font:{
fontSize:'24dp',
fontWeight:'bold'
},
height:'auto',
left:'10dp',
top:'5dp',
color:'#000',
touchEnabled:false
});
offerName = json[i].offerName;
nickLabel = Ti.UI.createLabel({
//text:'"' + fighter.nickname + '"', ***** fighter.nickname isn't defined anywhere so it is giving an error. You defined offerName right above it so I assume that it
// was the intended variable here.
text: offerName,
font:{
fontSize:'16dp'
},
height:'auto',
left:'15dp',
bottom:'5dp',
color:'#000',
touchEnabled:false
});
row.add(nameLabel);
row.add(nickLabel);
tableData.push(row);
}
table.setData(tableData);
},
onerror: function(e) {
Ti.API.debug("STATUS: " + this.status);
Ti.API.debug("TEXT: " + this.responseText);
Ti.API.debug("ERROR: " + e.error);
alert('There was an error retrieving the remote data. Try again.');
},
timeout:5000
});
xhr.open("GET", url);
xhr.send();
// Thought this might just be a test application, the table was never added to the window, so it would never be displayed so we can see the data. This is how you add the table.
win.add(table);
// I added code here to OPEN the window so I could see something displayed. Otherwise I just get the Appcelerator splash screen
win.open();
i'm trying to learn google api and i want to use marker clusterer. i create a database
from here and do whatever google said. my code is
`
<script src="markerclusterer.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var customIcons = {
restaurant: {
icon: 'images/pin1.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
bar: {
icon: 'images/pin2.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
}
};
function initialize() {
var markers = null;
var mcmarkers = [];
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(47.6145, -122.3418),
zoom: 12,
mapTypeId: 'satellite'
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP file
downloadUrl("phpsqlajax_genxml.php", function(data) {
var xml = data.responseXML;
markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var lat = parseFloat(markers[i].getAttribute("lat"));
var lng = parseFloat(markers[i].getAttribute("lng"))
var point = new google.maps.LatLng(lat, lng);
var html = "<b>" + name + "</b> <br/>" + address;
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
mcmarkers.push(marker);
var mc = new MarkerClusterer(map, mcmarkers);
bindInfoWindow(marker, map, infoWindow, html);
}
});
}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('POST', url, true);
request.send(null);
}
function doNothing() {}
//]]>
google.maps.event.addDomListener(window, 'load', initialize);
`
it works well when i open for the first time but when i zoom in or out there was no clusters except max zoom out. i couldn't figure it out. thanks for your help...
var mc = new MarkerClusterer(map, mcmarkers);
This line cannot be placed inside the for loop. but outside after it. I did exactly the same mistake:).
I am trying to open a new window by click a row in tableview in titanium.
this is my code,
function listpage(){
var tabgroup=Ti.UI.createTabGroup();
var win = Ti.UI.createWindow({
title:'Listpage',
backgroundColor:'#fff'
});
var dataArray = [{
title:'Sunday',
hasChild:true,
test:'detail'}];
for(var i=0;i<dataArray.length;i++){
dataArray[i].color = '#000';
dataArray[i].font = {fontWeight:'bold',fontSize:20};
}
var tableview=Ti.UI.createTableView({
data:dataArray
});
tableview.addEventListener('click',function(e){
if(e.rowData){
Ti.API.info(e.rowData.title);
var winEvent = Titanium.UI.createWindow({
backgroundColor:'#fff',
url:e.rowData.test
});
winEvent.open({animation:true});
}
});
win.add(tableview);
win.open();
}
detail.js code :
var self = Ti.UI.createWindow({
title:'Detail',
color:'#000',
backgroundColor:'white'
});
var label = Ti.UI.createLabel({
text:'Hai',
color:'#000'
});
self.add(label);
What to do to open detail.js window by clicking tableview row.
Thankyou.
Surendra, this is something similar to your problem. It opens a window while you clicks on each row.
Code is
var wndHome = Ti.UI.createWindow({
backgroundColor : 'white'
});
var tableView = Ti.UI.createTableView({
top : 0
});
var tabledata = [];
for(var i = 0 ; i < 10; i++){
var row = Ti.UI.createTableViewRow({
title : 'Title of window',
url : 'details.js'
});
tabledata.push(row);
}
tableView.data = tabledata;
wndHome.add(tableView);
wndHome.open();
tableView.addEventListener('click', function(e){
var wndNewWindow = Ti.UI.createWindow({
backgroundColor : '#999966',
url : e.rowData.url
});
wndNewWindow.open();
});
details.js file
var self = Ti.UI.currentWindow;
var label = Ti.UI.createLabel({
text:'Hai',
color:'#000'
});
self.add(label);
Here I've given the url to details.js. If you want to use different windows, then keep the file names in an array and while creating the tableViewRow, you can simply add the array element as the url(eg: fileArray = ['file1.js', 'file2.js','file3.js'];).
Try this and change your code as per your requirement.
i'm getting a JSON response properly but my problem is with showing it. what's wrong with my code.
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
// create base UI tab and root window
var win1 = Titanium.UI.createWindow({
title : 'Main Window',
backgroundColor : '#fff'
});
var listUrl = "http://magadhena.com/test/list.php?FCODE=5&USERID=1";
var NumberOfLists = [];
var lists;
var tableData = [];
var table = Ti.UI.createTableView({
top : 40,
left : 10,
width : 300
});
var txt1 = Titanium.UI.createTextField({
top : 10,
left : 10,
width : 250
});
var button1 = Ti.UI.createButton({
top : 10,
left : 270,
width : 30
});
var xhr = Ti.Network.createHTTPClient();
xhr.setTimeout(3000);
xhr.onload = function() {
lists = eval('(' + this.responseText + ')');
for(var i = 0; i < lists.length; i++) {
var userId = lists[i].userid;
// The userID
var listId = lists[i].listid;
// The ListID
var listName = lists[i].listname;
// The ListName
var Object1 = new list(userId, listId, listName);
// Ti.API.log("Object is ",Object1.listId);
NumberOfLists.push(Object1);
// Ti.API.log("the size of the Json array is" , NumberOfLists.length);
}
};
xhr.open("GET", listUrl);
xhr.send();
for(var i = 0; i < NumberOfLists.length; i++) {
var row = Ti.UI.createTableViewRow({
title : NumberOfLists[i].listName
});
Ti.API.log("populating the data table ", NumberOfLists[i].toString);
tableData.push(row)
};
// Ti.API.log("the size of table data is ", tableData.length);
table.setData(tableData);
win1.add(table);
win1.add(txt1);
win1.add(button1);
// Opening Window1
win1.open();
///// List Objects
function list(userid, listid, listname) {
this.userId = userid;
this.listId = listid;
this.listName = listname;
}
You need to put table.setData() into xhr.onload function. Since you defined the code outside of the function, NumberOfLists is empty until xhr.onload function executed