Access to the rows of the table is not correct. Android 2.1

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

Good day!!!

I have:

  • Application type: mobile
  • Titanium SDK: 2.1.1GA
  • Platform & version: Android 2.1,
  • Device: Android emulator, Motorola Xoom and couple android devices
  • Host Operating System: Windows 7
  • Titanium Studio: Titanium Studio, build: 2.1.1.201207271312

I have a table. In the rows of the table are some data. The structure of a table row:

row(Ti.UI.TableViewRow) > wrapperViw(Ti.UI.View) > leftText(Ti.UI.Label) + rightText(Ti.UI.Label)

from time to time text in the rows of the table should be changed.

When clicking on the row I need to change the color in leftText(Ti.UI.Label) and rightText(Ti.UI.Label)

I'm doing it this way:

//create table
var listOfAnswers = Ti.UI.createTableView({
   //some staff
});
 
listOfAnswers.addEventListener('click', markRowAsSelected);
 
//Click handler
function markRowAsSelected(e){    
    e.row.selected = e.row.selected?false:true;
    e.row.children[0].children[0].color = e.row.selected?'#9608a3':'#000000';
    e.row.children[0].children[1].color = e.row.selected?'#9608a3':'#000000';    
}
 
//create data for the table. return array of Ti.UI.tableViewRow
function createListOfAnswersData(/*array*/answers){
    if(!!app.game.listOfAnswers && app.game.listOfAnswers.length!==0){
        // If we have a ready-made UI items, then simply update the text in them
        refreshListOfAnswersUI(answers);
    }else{
        // If we do not - they are creating
        createListOfAnswersUI(answers);
    }       
    return app.game.listOfAnswers
};
 
 
function refreshListOfAnswersUI(/*array*/answers){
   for (var i = 0, j = app.game.listOfAnswers.length; i < j; i++) {
        app.game.listOfAnswers[i].selected = false;
        app.game.listOfAnswers[i].children[0].children[0].text = answers[i].text;
        app.game.listOfAnswers[i].children[0].children[1].text = answers[i].points;
        app.game.listOfAnswers[i].children[0].children[0].color = '#000000';
        app.game.listOfAnswers[i].children[0].children[1].color = '#000000';
   }
}
 
function createListOfAnswersUI(/*array*/answers){
    app.game.listOfAnswers = [];    
    for(var i=0, j=answers.length; i<j; i++){
        app.game.listOfAnswers.push(
                createAnswerRow()
        );       
    }
}
 
 
function createAnswerRow(answerData, bgIndex, answerWidth, answerLeft, index){    
    var answerWrapper = Ti.UI.createView({
   //some staff
   });
var answerText = Ti.UI.createLabel({
   //some staff
   text:answerData.text
});
var answerPoints = Ti.UI.createLabel({
   //some staff
   text:answerData.points
});
 
var row = Ti.UI.createTableViewRow({
    //some staff    
    className:'answerRow'
});        
    answerWrapper.add(answerText);
    answerWrapper.add(answerPoints);
    row.add(answerWrapper);
    return row
}
 
function refreshListOfAnswers(data){
    listOfAnswers.setData(data)
}
 
refreshListOfAnswers(createListOfAnswersData(currentQuestion.answers));
On android 2.2 and above its work perfect, but on 2.1 i have the problem.

For the first time everything works fine. The second time the function markRowAsSelected is executed, but strange. If you click on the first row of the table, the text color will change in the tenth row. If you click on the second row of the table, the text color will change in the ninth row. And so on. That is, there is an impression that the list of table rows reversed. And this applies only to change the text color, becose when I click on the first row of the table in the debugger I see e.index == 0, but the text color change in the tenth row of the table.

The third time, again, the function markRowAsSelected works fine, for the fourth time she is not working properly. I have now is a strange alternation.

In what may be the problem?

Regards, Andrew.

1 Answer

try this so you are accessing the row objects directly

var row = Ti.UI.createTableViewRow({
    //some staff    
    className:'answerRow'
});        
answerWrapper.add(answerText);
answerWrapper.add(answerPoints);
 
 
// associate objects directly to row so you dont have to look for them later
row.answerText = answerText;
row.answerPoints = answerPoints;
 
row.add(answerWrapper);

— answered 10 months ago by Aaron Saunders
answer permalink
4 Comments
  • Hi, Aaron! Thank you for your response. It works exactly the same as my code. The text color is changed, but in the wrong rows. If you click on the first row of the table, the text color will change in the tenth row. If you click on the second row of the table, the text color will change in the ninth row. And so on.

    — commented 10 months ago by Andrew Vesel4ak

  • I tried this way:

    function createAnswerRow(answerData){
     
           var answerWrapper = Ti.UI.createView({
                  //some staff
           });
           var answerText = Ti.UI.createLabel({
                  //some staff
                  text:answerData.text
           });
           var answerPoints = Ti.UI.createLabel({
                  //some staff
                  text:answerData.points
           });
     
           var row = Ti.UI.createTableViewRow({
                  //some staff    
                  className:'answerRow'
           });        
     
     
            row.addEventListener('click', function(){
                row.selected = row.selected?false:true;
                answerText.color = row.selected?'#9608a3':'#000000';
                answerPoints.color = row.selected?'#9608a3':'#000000';
            });        
            answerWrapper.add(answerText);
            answerWrapper.add(answerPoints);
            row.add(answerWrapper);
     
            return row
        }
    The same result

    — commented 10 months ago by Andrew Vesel4ak

  • the eventListener should be on the table not the row

    — commented 10 months ago by Aaron Saunders

  • Show 1 more comment

Your Answer

Think you can help? Login to answer this question!