1
\$\begingroup\$

I develop an android native application using phonegap and jquery mobile. I've made a function to create a listview style dynamically using looping for... When I run the function for the first time, it works, the listview style is working, and match with the content. but when I press back button and run the function again, the style change (the listview style is gone) but the content still there. is my function wrong? how to solve this problem?

the screenshoot for more explanation:

the codes for more detail:

function detail(kodenegara, koderesult)
        {
            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                url: "http://10.80.3.73/webservice/Service1.svc/json/weeklyflash/"+kodenegara,
                dataType: "json",
                success:function(data){
                    var result = koderesult;

                    maks = -1;
                    for(i = 0; i < data[result].length; i++) {
                        if(data[result][i].bulan > maks) 
                        maks = data[result][i].bulan;
                    }

                    var loop_tipe = countTypesForBulan(data[result], maks);
                    var innerHtml = "";

                    for (i = 0; i < loop_tipe; i++){
                        a=i+loop_tipe;
                        b=a+loop_tipe;
                        innerHtml += 
                        "<div data-role='collapsible' data-collapsed='true'>"+
                        "<h3>"+data[result][i].type+"</h3>"+
                        "<table width='100%' border='1'>"+
                            "<tr>"+
                                "<td>&nbsp;</td>"+
                                "<td style='text-align: center'>"+data[result][i].bulan+"/2012</td>"+
                                "<td style='text-align: center'>"+data[result][a].bulan+"/2012</td>"+
                                "<td style='text-align: center'>"+data[result][b].bulan+"/2012</td>"+
                            "</tr>"+
                            "<tr>"+
                                "<td>Cash</td>"+
                                "<td style='text-align: right'>"+data[result][i].uang+"</td>"+
                                "<td style='text-align: right'>"+data[result][a].uang+"</td>"+
                                "<td style='text-align: right'>"+data[result][b].uang+"</td>"+
                            "</tr>"+
                            "<tr>"+
                                "<td>Qty</td>"+
                                "<td style='text-align: right'>"+data[result][i].total+"</td>"+
                                "<td style='text-align: right'>"+data[result][a].total+"</td>"+
                                "<td style='text-align: right'>"+data[result][b].total+"</td>"+
                            "</tr>"+
                        "</table>"+
                        "</div>";
                    }
                    $('#tipe').html(innerHtml);
                    $.mobile.changePage("#detail", "slide", false, true);

                    //show the page
                },
                error: function () { 
                    alert("ERROR"); 
                }
            });
        }

        function countTypesForBulan(resultArray, bulanVal) {
            var i,
                types,
                count = 0;
            for (i=0, types = {}; i < resultArray.length; i++)
                if (resultArray[i].bulan === bulanVal && !types[resultArray[i].type]) {
                types[resultArray[i].type] = true;
                count++;
            }
            return count;
        }
\$\endgroup\$
4
  • \$\begingroup\$ Where is the code for review? If this is more of a problem of broken code I'd suggest Stack Overflow otherwise please help us by adding some code. Thanks. EDIT:- I should have prefaced that with .... "I'm too lazy to go to paste bins for code" ? \$\endgroup\$ Commented Jul 5, 2012 at 2:01
  • \$\begingroup\$ @JamesKhoury well, my code is working but I think there is some bugs in my code and need to be review. I've update my thread now, thank you..sorry :p \$\endgroup\$ Commented Jul 5, 2012 at 2:28
  • \$\begingroup\$ thanks its easier to read code when its in the thread. I'm not sure whats wrong with this. Could you also provide a sample response from that url? \$\endgroup\$ Commented Jul 5, 2012 at 3:02
  • \$\begingroup\$ here is the url result: {"GetReportIdResult":[{"bulan":"4","total":"1728","type":"CHEESE1K","uang":"8796383"},{"bulan":"4","total":"572476","type":"ESL","uang":"5863408410"},{"bulan":"5","total":"4761","type":"CHEESE1K","uang":"134877865"},{"bulan":"5","total":"648663","type":"ESL","uang":"6645764498"},{"bulan":"6","total":"5762","type":"CHEESE1K","uang":"293393832"},{"bulan":"6","total":"594942","type":"ESL","uang":"6088671790"},]} \$\endgroup\$ Commented Jul 5, 2012 at 3:29

1 Answer 1

1
\$\begingroup\$

I can't see anything wrong with your code in terms of why it gives you different styles. I would however ensure you have a consistent coding standard.

For example my coding standards usually include:

  1. Always declare variables. Variable maks is never declared here (I'm going to assume it is never declared.) Change it to var maks = -1;

  2. Always use { and }. This will ensure you don't screw up a block.

    if(data[result][i].bulan > maks) 
        maks = data[result][i].bulan;
    

    would become:

    if(data[result][i].bulan > maks)
    {
        maks = data[result][i].bulan;
    }
    

    Its just proofing for us who make mistakes from time to time.

  3. make local variables where possible

    if (resultArray[i].bulan === bulanVal && !types[resultArray[i].type]) {
        types[resultArray[i].type] = true;
        count++;
    }
    

    could be:

     var current = resultArray[i];
     if (current.bulan === bulanVal && !types[current.type]) {
        types[current.type] = true;
        count++;
    }
    

    It means less look ups. Most of the time its easier to read.

\$\endgroup\$
9
  • \$\begingroup\$ okay, I'll update my code as your advice. Thank you very much :)) \$\endgroup\$ Commented Jul 5, 2012 at 5:01
  • \$\begingroup\$ @blankon91 It might help to see the outer for loop that calls this code. There might be something there that is the root of your problem. \$\endgroup\$ Commented Jul 5, 2012 at 6:00
  • \$\begingroup\$ here is the outer that call this code pastie.org/4201837 \$\endgroup\$ Commented Jul 5, 2012 at 6:12
  • \$\begingroup\$ @blankon91 Your original post seemed to suggest this was created dynamically in a for.. loop \$\endgroup\$ Commented Jul 5, 2012 at 6:14
  • \$\begingroup\$ yes, I want it created dynamically listview \$\endgroup\$ Commented Jul 5, 2012 at 6:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.