Categories
discuss

Mustache looping error when trying to display data

I am having a problem doing a loop in mustache. Basically a usr has the ability to add options to products. Each option can have more then 1 choice. Also they come together Choice Name + Price. I get the following error:

Uncaught Error: Unopened section: choices

My code:

var choices = new Object();

    $("[name='choice_name']").each(function(){
        var c_name = $(this).val();
        $("[name='choice_price']").each(function(){
            var c_price = $(this).val();
            choices.choice_name = c_name;
            choices.choice_price = c_price;
        });
    });

    console.log(choices);

    var templateData = {
        name: $("[name='option_name']").val(),
        type: $("[name='option_type']").find("option:selected").val(),
        choices: choices
    };

    $.get(default_url+"js_temp/options_widget.html", function(templates){
        $('.current_options').append(Mustache.render(templates, templateData));
    });

HTML:

<div>
  <p class="pull-right"><i class="icon icon-pencil"></i><br /><i class="icon icon-trash"></i></p>
    <p><strong>Option Name:</strong> {{option_name}}</p>
    <p><strong>Option Type:</strong> {{option_type}}</p>
    <hr>
    {{choices}}
    <div class="row-fluid">
      <div class="span7"><p><strong>Choice Name:</strong> {{choice_name}}</p></div>
      <div class="span5"><p><strong>Price:</strong> {{choice_price}}</p></div>
    </div>
    {{/choices}}
</div>

I think I can’t achieve the correct format for the choices object. What am I doing wrong?

Thank you.

Answer

The syntax for a loop is {{#choices}} {{prop}} {{/choices}} — looks like you are missing the #.

{{#choices}}
    <div class="row-fluid">
      <div class="span7"><p><strong>Choice Name:</strong> {{choice_name}}</p></div>
      <div class="span5"><p><strong>Price:</strong> {{choice_price}}</p></div>
    </div>
{{/choices}}

Also, in constructing choices, you’re overwriting each object — I think you want to push each one to the array. Try this:

var choices = [];

$("[name='choice_name']").each(function(){
    var c_name = $(this).val();
    $("[name='choice_price']").each(function(){
        var c_price = $(this).val();
        choices.push({ 
            choice_name: c_name, 
            choice_price: c_price 
        });
    });
});

See here: http://jsfiddle.net/U6pLT/1/

Categories
discuss

Where do I put the txt file that I want to read in Java?

If I am building an application in Eclipse using Java, and I want this application to read from a txt file, where should I put the txt file in the package in order to not have to specify its path (so that I can just say “file.txt”). I thought that it was supposed to go in the src folder, but it’s not working.

Answer

Right click the project folder and click New -> file. The file will be in the Project folder and not in the source folder.

Categories
discuss

Whether a bound service or customized threads when download something?

I’m working on an Android project, in which I need to load some images to a GridView. The images could be in the cache, if cache missing, then query the Internet server. My design is to use two threads to do the loading task. One thread for reading cache and one thread for downloading. So there are 3 threads including the UI one. Each of them has its own message queue and uses Handler to communicate.

My question is whether I should use bound service in this situation instead? Actually I have realized the design above and it seems nothing wrong but GC is working very hard, which can be inferred from the logcat.

Another issue is that several threads exist when I use DDMS to monitor the threads. This is because the same loading mechanism is used in several Activities. I have let the threads quit its message loop while onPause() is called, I’m sure only two of them are alive in the same time. But I can see all of them in DDMS. (BTW, why the threads still exist? I have let thread = null;)

So in a word, my question is: Could this loading task benefit from a bound service?

Answer

So why are you using multiple threads? You need to load an image in the background, and when done, display it in the UI. It really doesn’t matter to the UI where the images comes from. And using multiple threads doesn’t make it faster, just consumes more memory. Just use a single background thread: first it hits the cache, then downloads if no hit.

Services are for when you want do something that doesn’t need a UI. If you need to update the UI in real time, a service doesn’t make much sense.

Categories
discuss

How to get a list of the activity history stack?

I’m struggling with an issue in my app. I’d like to provide a way to list the history of the previous opened activities.

I think there are two potential solutions but I’m not sure to find a stable way to achieve any of them.

  • Somehow use a function from the SDK. I tried using getPackageManager() and getActivityManager() but I can’t find any suitable solution
  • Implement my own history but then I’ll have to override startActivity and I don’t really want to go down that way as an activity can be started in other multiple ways.

So is there a way to do that using a function from the SDK?

Many thanks.

EDIT:

I’ve got another idea, it’s a bit different. Would it be possible to listen to “start/finish activity” events with some kind of receiver to a specific app (mine) and keep track of the stack history?

Answer

So is there a way to do that using a function from the SDK?

No, sorry.

Would it be possible to listen to “start/finish activity” events with some kind of receiver to a specific app (mine) and keep track of the stack history?

You can use registerActivityLifecycleCallbacks() on Application, if you are only going to support API Level 14 and higher (a.k.a., Android 4.0+).

Categories
discuss

Android SlidingMenu errors When Referencing ABS

I am trying to use a SlidingMenu (https://github.com/jfeinstein10/SlidingMenu/) in my app. My first issue was that the project was using MATCH_PARENT, which wouldn’t compile for me. The minSDK was set to 5, and the targetSDK was 17. I tried changing the min to 8, which is when MATCH_PARENT was introduced, to no luck.

After replacing all of the MATCH_PARENT’s with FILL_PARENT’s, which both compile to -1, the project seemed fine. I then proceeded to make it use ABS as per the developer’s instructions on his github. Whenever I reference ABS though, I get many errors such as:

[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvalues-v14abs__styles.xml:121: error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Holo.Widget.PopupMenu.Small'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvalues-v11abs__themes.xml:4: error: Error retrieving parent for item: No resource found that matches the given name 'android:Theme.Holo'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvalues-v11abs__themes.xml:6: error: Error: No resource found that matches the given name: attr 'android:windowActionBar'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvalues-v14abs__themes.xml:4: error: Error retrieving parent for item: No resource found that matches the given name 'android:Theme.Holo'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvalues-v11abs__themes.xml:8: error: Error retrieving parent for item: No resource found that matches the given name 'android:Theme.Holo.Light'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvalues-v11abs__themes.xml:10: error: Error: No resource found that matches the given name: attr 'android:windowActionBar'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvalues-v14abs__themes.xml:6: error: Error retrieving parent for item: No resource found that matches the given name 'android:Theme.Holo.Light'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvalues-v14abs__themes.xml:8: error: Error retrieving parent for item: No resource found that matches the given name 'android:Theme.Holo.Light.DarkActionBar'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvalues-v14abs__themes.xml:14: error: Error: No resource found that matches the given name: attr 'android:actionBarWidgetTheme'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvalues-v14abs__themes.xml:22: error: Error: No resource found that matches the given name: attr 'android:windowActionBar'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvalues-v14abs__themes.xml:26: error: Error: No resource found that matches the given name: attr 'android:windowActionBar'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvaluesabs__themes.xml:196: error: Error: No resource found that matches the given name: attr 'android:windowActionBar'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvaluesabs__themes.xml:197: error: Error: No resource found that matches the given name: attr 'android:windowActionModeOverlay'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvaluesabs__themes.xml:198: error: Error: No resource found that matches the given name: attr 'android:windowCloseOnTouchOutside'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvalues-v14abs__themes.xml:30: error: Error retrieving parent for item: No resource found that matches the given name 'android:Theme.Holo.Dialog'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvaluesabs__themes.xml:222: error: Error: No resource found that matches the given name: attr 'android:windowActionBar'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvaluesabs__themes.xml:223: error: Error: No resource found that matches the given name: attr 'android:windowActionModeOverlay'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvaluesabs__themes.xml:224: error: Error: No resource found that matches the given name: attr 'android:windowCloseOnTouchOutside'.
[2012-12-02 13:53:39 - LibrarySlidingMenu] C:UsersRynoJvRDropboxAndroidLibraryActionBarSerlockresvalues-v14abs__themes.xml:32: error: Error retrieving parent for item: No resource found that matches the given name 'android:Theme.Holo.Light.Dialog'.

As far as I can tell, SlidingMenu is throwing the error because of some of the XML in ABS, meaning that R won’t generate, and I can’t use SlidingMenu.

Answer

Go to the properties . enter image description here

click on the most recent target platform (which also has google maps for mapActivity). In this image its 4.2 Google Apis

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..