Getting checked values from Table view.

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

Hi, I created table view , each row containing label and Switch box. I'm getting values from database and assigning to the label text. This is fine. I created one save button in one that window. Here, I performed some checks on the check boxes according to the label text, this is my requirement. I have checked some check boxes of the table view rows. Here after checking , I want to save checked and unchecked values of those row label values into separate tables of my database i.e checked table and unchecked table. When I'm clicking the save button , those values will stored into related table. Please,any one help me on this..?? This is my code.

var save = Titanium.UI.createButton({title:'Save',height:'40',bottom:02,width:100});
var db = Titanium.Database.install('mydata.sqlite','maintable');
var row1 = db.execute('SELECT content FROM maintable'); 
var tableview = Titanium.UI.createTableView({bottom:40});
var CustomData = [];
while(row1.isValidRow())
    {
     CustomData.push({name:'' + row1.fieldByName('content') + '', path:''});  
    row1.next();
    };
//reading values from CustomData Array and adding to tableview  
var data1=[];
for (var i = CustomData.length - 1; i >= 0; i--) {
var row = Titanium.UI.createTableViewRow();
 
var name = Titanium.UI.createLabel({
text:CustomData[i].name,
font:{fontSize:13,fontWeight:'bold'},
width:'auto',textAlign:'left',top:10,left:10,height:24
});
 
var checkb = Titanium.UI.createSwitch({
    style:Titanium.UI.Android.SWITCH_STYLE_CHECKBOX,
    value:false,
    left:270
});
row.add(name);
row.add(checkb);
row.className = 'mobile_switch';
data1.push(row);
};
save.addEventListener('click',function(e)
{
for (var i = data1.length - 1; i >= 0; i--) {
  if (row[i].check == true) {alert("Checked")} else{alert("unchecked")};
};
})
tableview.setData(data1);
win1.add(tableview);
win1.add(save);
Here, when I click the save button after checking some row, all alert messages displays only unchecked MSG's. I know the reason, that for loop reeds the previous values of the table vies, i.e unchecked. How can I get the tables values, after performing some checked options on the table view rows. Please help me.. Regards by, Lokesh.

— asked 2 years ago by lokesh g
0 Comments

1 Answer

Looks like you want to be reading the value of this switch not the row.

Add the switch to the row properties

row[i].switch = checkdb;
then in your save function
if (row[i].switch.value == true) {alert("Checked")} else{alert("unchecked")};

— answered 2 years ago by Martin Slater
answer permalink
2 Comments
  • switch was not working in the if condition, i'm trying with switch name also.

    — commented 2 years ago by lokesh g

  • to be sure that last line should be

    if(data1[i].switch.value == true) ....
    didn't spot your error in the original

    — commented 2 years ago by Martin Slater

Your Answer

Think you can help? Login to answer this question!