简体   繁体   中英

Label text is not updating in tableview children in titanium android.but work's in IOS

I have tried to update/change the lebel in android using titanium.but the value is not showing on the text .but am getting the updated value in the alert.But the ios is working perfectly.

 var itemcounttext = Ti.UI.createLabel({
 top: 5,
 color: "#000000",
 left:10,
 text:data[i].itemCount,
 width: Ti.UI.SIZE,
 height: Ti.UI.SIZE,
 });
var additem = Ti.UI.createImageView({
image: "/images/plus.jpg",
top: 5,
width: Ti.UI.SIZE,
left:10,
height: Ti.UI.SIZE,
});
adddeleteitemview.add(additem);
additem.addEventListener('click', function(e)
    {
        var item=e.source.getParent();
        squantity = e.source.squantity;
            squantity = Number(squantity) + 1;
            item.children[1].text = squantity;
            alert(item.children[1].getText());

Here am getting the alert with correct updated value.But the it's not showing in the label. Can you give me a idea to resolve this problem in android.

EDIT:

From VRK comment i have tried this also.But it's not working.but am getting the alert correctly.

item.children[1].setText(squantity);

EDIT:

I tried with jsplaine answer.But i can't get the solution. Here i have created the tableview .in this tableview row we are creating the view.in that view i have adidng the additem,itemcounttext values.if we are clicking the additem means need to change the itemcounttext value.This is a flow.

like below screenshot is children view of my app tableview:

productname

remove     itemcount   add
image      text        image

this three image,text,image values are added in one view.That's why am adding the code for getting the parent view while clicking add image:

var item=e.source.getParent();

here am getting the parent.also am wrote the below code for getting the label for this view:

item.children[1].text

If am clicking the add image, am getting the label value is incresed by 1 correctly .am verified with this code alert(item.children[1].getText()) ;. but the updated value is not showing.This is my exact doubt.

EDIT:

This is my full source code.

 dataArray = [];       
 $.ViewCartItemslist_total_value.text = totalamount;
 for( var i=0; i<data.length; i++){ 

//creating the tableviewrow

 var row = Ti.UI.createTableViewRow({
 layout : 'horizontal',
 top:5,
 width: "100%",
 height: Ti.UI.SIZE,
 });
 row.add(Ti.UI.createImageView({
 image: data[i].image,
 top: 5,
 width: '50',
 height: Ti.UI.SIZE,
 }));
row.add(Ti.UI.createLabel({
text: data[i].name,
 top: 5,
 width: 180,
 font: { fontSize: '10dp' },
 color: '#040404',
 wordWrap: true,
 height: Ti.UI.SIZE,
 ellipsize: true
 }));

//creating the view inside of each every row of tableviewrow

 var adddeleteitemview = Ti.UI.createView({
 width: Ti.UI.SIZE,
 height: Ti.UI.SIZE,
 layout : 'horizontal',
 left:10,
 borderColor:"gray",
 borderRadius:"10"
 });
 var removeitem = Ti.UI.createImageView({
 image: "/images/minus.jpg",
 top: 5,
 left:10,
 width: "15%",
 height: Ti.UI.SIZE,
 });
 adddeleteitemview.add(removeitem);
 var itemcounttext = Ti.UI.createLabel({
 top: 5,
 color: "#000000",
 left:10,
 text:data[i].itemCount,
 textAlign:'center',
 width: "15%",
 height: Ti.UI.SIZE,
 });
 adddeleteitemview.add(itemcounttext);

 var additem = Ti.UI.createImageView({
 image: "/images/plus.jpg",
 top: 5,
 width: "15%",
 left:10,
 squantity : data[i].itemCount,
 spprice :data[i].itemPrice,
 height: Ti.UI.SIZE,
 });
 adddeleteitemview.add(additem);
 additem.addEventListener('click', function(e)
    {
        var item=e.source.getParent();
        spprice = e.source.spprice;
        if(item.children[1].getText() == e.source.squantity){
        squantity = e.source.squantity;
          totalqty = Number(totalqty) + Number(1);
            $.ViewCartItemslist_header_cart.text = totalqty;
            totalamount = Number(totalamount) + Number((spprice));
            squantity = Number(squantity) + 1;
            item.children[1].text = squantity;
           // item.itemcounttext.text = squantity;
          // item.itemcounttext.setText(squantity);
           // item.children[1].setText(squantity);
            alert(item.children[1]+" "+item.children[1].getText());
            $.ViewCartItemslist_total_value.text = totalamount;
            totalprice = Number(spprice) * squantity;
        }
           else {
                squantity = item.children[1].getText();
            totalqty = Number(totalqty) + Number(1);
            $.ViewCartItemslist_header_cart.text = totalqty;
            totalamount = Number(totalamount) + Number((spprice));
            squantity = Number(squantity) + 1;
            item.children[1].text = squantity;
            item.children[1].setText(squantity);
            alert(item.children[1].getText());
            $.ViewCartItemslist_total_value.text = totalamount;
            totalprice = Number(spprice) * squantity;
            }
           });
       row.add(adddeleteitemview); 

      dataArray.push(row);
    row.addEventListener('click', function(e) {
    });
    $.ViewCartItemstableView.setData(dataArray);
     }

Instead of walking the parent/child tree, just make itemcounttext a property of additem:

var itemcounttext = Ti.UI.createLabel({
 top: 5,
 color: "#000000",
 left:10,
 text:data[i].itemCount,
 width: Ti.UI.SIZE,
 height: Ti.UI.SIZE
});

var additem = Ti.UI.createImageView({
 image: "/images/plus.jpg",
 top: 5,
 width: Ti.UI.SIZE,
 left:10,
 height: Ti.UI.SIZE,
});

additem.countTextLabel = itemcounttext;

adddeleteitemview.add(additem);

additem.addEventListener('click', function(e) {
 var item=e.source;
 var squantity = e.source.squantity;
 squantity = Number(squantity) + 1;
 item.countTextLabel.setText(squantity);
 alert(item.countTextLabel.getText());
});

I struggled with this for the better part of an afternoon. Hopefully this helps someone.

In my case I had a tableview with just 1 custom row by design. Clicking the row allowed me to choose a new value (name and description) from a different tableview, and then update the first tableview custom row data with the newly selected values.

For the problematic tableview, I was using setData, but the custom rows are fairly complex so I didn't want to have to rebuild one row of one of these complex rows. Here is what worked for me... I just updated the labels in the data array that are contained in a wrapper view that is contained in the row. This way rather than rebuilding all of the data array from scratch, I was able to modify the array and then just set data again. dataStatus[0].children[0].children[0].text = sice.row.name;
dataStatus[0].children[0].children[1].text = sice.row.statusdescription; tableviewStatus.setData(dataStatus);

I only have 1 row so dataStatus[0] addresses that row. Then the first child is the wrapper view, and I counted my layout and the 1st and 2nd child of the wrapper are the labels I needed to update. Then after I modified the array, I just redid setData. Worked like a champ.

Hope it helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM