Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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;
        }
share|improve this question

closed as off-topic by Jamal Aug 19 '14 at 15:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. Such questions may be suitable for Stack Overflow or Programmers. After the question has been edited to contain working code, we will consider reopening it." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.

    
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" ? –  James Khoury Jul 5 '12 at 2:01
    
@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 –  blankon91 Jul 5 '12 at 2:28
    
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? –  James Khoury Jul 5 '12 at 3:02
    
here is the url result: {"GetReportIdResult":[{"bulan":"4","total":"1728","type":"CHEESE1K","uang":"879‌​6383"},{"bulan":"4","total":"572476","type":"ESL","uang":"5863408410"},{"bulan":"‌​5","total":"4761","type":"CHEESE1K","uang":"134877865"},{"bulan":"5","total":"648‌​663","type":"ESL","uang":"6645764498"},{"bulan":"6","total":"5762","type":"CHEESE‌​1K","uang":"293393832"},{"bulan":"6","total":"594942","type":"ESL","uang":"608867‌​1790"},]} –  blankon91 Jul 5 '12 at 3:29

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
okay, I'll update my code as your advice. Thank you very much :)) –  blankon91 Jul 5 '12 at 5:01
    
@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. –  James Khoury Jul 5 '12 at 6:00
    
here is the outer that call this code pastie.org/4201837 –  blankon91 Jul 5 '12 at 6:12
    
@blankon91 Your original post seemed to suggest this was created dynamically in a for.. loop –  James Khoury Jul 5 '12 at 6:14
    
yes, I want it created dynamically listview –  blankon91 Jul 5 '12 at 6:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.