How can i check the value of my checkbox in android

You must Login before you can answer or comment on any questions.

I'm having a lot of difficulty in checking the state of my checkbox with the android switchbox. When i click on the checkbox it tells me it cannot find the source i don't know why. This is the code that i have:

var merken = [  { title: "Almere", color: "black", height: 75,}, 
                { title: "Amsterdam", color: "black", height: 75,}, 
        { title: "Roberto botticelli", color: "black", height:  75,  },];
 
 
Ti.include('data.js');
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
 
// create tab group
var tabGroup = Titanium.UI.createTabGroup();
 
 
// create base UI tab and root window
 
var win1 = Titanium.UI.createWindow({  
    title:'Tab 1',
    backgroundColor:'#fff'
});
var tab1 = Titanium.UI.createTab({  
    icon:'KS_nav_views.png',
    title:'Deals',
    window:win1
});
 
var label1 = Titanium.UI.createLabel({
    color:'#999',
    text:'I am Window 1',
    font:{fontSize:20,fontFamily:'Helvetica Neue'},
    textAlign:'center',
    width:'auto'
});
 
win1.add(label1);
 
 
// create controls tab and root window
 
    var win2 = Titanium.UI.createWindow({  
        title:'Tab 2',
        backgroundColor:'#fff',
        layout:"vertical"
    });
    var tab2 = Titanium.UI.createTab({  
        icon:'KS_nav_ui.png',
        title:'Instellingen',
        window:win2
    });
 
    var tableData = [];
    for(var i=0,ilen=merken.length; i<ilen; i++){
    var thisObj = merken[i];
    var thisLabelMerken = Ti.UI.createLabel({
      color:"black",
      font:{ fontSize: 16, fontWeight:"bold" },
      text:thisObj.title,
      width:'auto',
      textAlign:'left',
      top:10,
      left:10,
      height:20,
 
    });
 
    var thisCheckbox = Titanium.UI.createSwitch({
        style:Titanium.UI.Android.SWITCH_STYLE_CHECKBOX,
        value:true,
        left:400
 
    });
 
 
    var thisRow = Ti.UI.createTableViewRow({
      layout:"vertical",
      selectedColor:"black",
      height:75,
 
    });
 
    thisRow.add(thisLabelMerken);
    thisRow.add(thisCheckbox);
    tableData.push(thisRow);
 
    }
 
 
 
    var headerLabel = Ti.UI.createLabel({
      backgroundColor:'#035385',
      color:"white",
      font:{ fontSize: 30, fontWeight:"bold" },
      text:"Favoriete Merken",
      textAlign:"center",
      height:45,
      width:'500'
    });
 
 
 
    var table = Ti.UI.createTableView({
      backgroundColor:"white",
      data:tableData,
      headerView:headerLabel,
      separatorColor:"black",
      top:0,
      width:'auto'
    });
 
 
    table.addEventListener('click', function(e)
{
 
//var value = checkb[i].value;
    for (var i=0,ilen=merken.length; i<ilen; i++) {
       if (table.tableData[i].thisRow.thisCheckbox.value) {alert("true");} else{alert("false");};
    };
 
});
 
 
win2.add(table);
 
//  add tabs
tabGroup.addTab(tab1);  
tabGroup.addTab(tab2);  
 
// open tab group
tabGroup.open();

— asked 3 years ago by jan young
0 Comments

1 Answer

Accepted Answer

Hi.

First, add checkbox reference to table view row after it is created:

thisRow.thisCheckbox = thisCheckbox;
Then in table view click event handler simply do this:
table.addEventListener('click', function(e)
{
    Ti.alert(e.row.thisCheckbox.value);
});
Basically, in table view click event handler you have reference to clicked row and you just need to get checkbox easily and that's why I have added it's reference to the table view row.

— answered 3 years ago by Ivan ┼ákugor
answer permalink
4 Comments
  • Hi thanks for your fast response, I've tested it and it WORKS! I've been struggling with this for days, can't believe the answer was that simple. I was wondering if some of the checkboxes get clicked and i want to store the changed state of these checkboxes using Ti.App.Properties do i use something like this to do the trick:

    // First i add the reference
     
    thisRow.thisLabelMerken = thisLabelMerken;
     
    // Then the eventlistener  
     
    table.addEventListener('click', function(e)
    {
    Ti.App.Properties.setBool('e.row.thisLabelMerken.text',e.row.thisCheckbox.value);
     
    });
    Will this do the trick?

    — commented 3 years ago by jan young

  • No, that way you'll only store one value (the last one clicked) because you have put 'e.row.thisLabelMerken.text' in quotes. Remove quotes and it should store all checkbox values by "e.row.thisLabelMerken.text" value as a key.

    — commented 3 years ago by Ivan ┼ákugor

  • Ok,thanks a lot!

    — commented 3 years ago by jan young

  • Show 1 more comment

Your Answer

Think you can help? Login to answer this question!