Categories
discuss

Google Custom Search on submit

I would like to customize my search form. I am using Google Search Service and have it linked to my domain and so on.

I chose the two column layout in the Control Panel, but however, I want to do something onSubmit of the form.

So I tried to put an actionlistener in jQuery into the form, however does not work.

Then I thought google certainly provides something for that. And yes they do. It is called:

setOnSubmitCallback()

http://code.google.com/apis/websearch/docs/reference.html

Unfortunately I dont get it.

So far I have:

google.load('search', '1', {language : 'en', style : google.loader.themes.MINIMALIST});

                    function initialize()
                    {
                        var searchControl = new google.search.CustomSearchControl('017998360718714977594:j6sbtr-d6x8');
                        searchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);

                        var options = new google.search.DrawOptions();
                        options.setSearchFormRoot('cse-search-form');

                        searchControl.draw('cse', options);
                    }

                    google.setOnLoadCallback(initialize);

So i have two divs: #cse-search-form for the form and #cse for the results

#cse is in another div #searchResults, that is hidden and here it comes:

I want to open #searchResults in a dialog from jQuery UI.

$("#searchResults").dialog( { minWidth: 750, minHeight: 750 } );

Which will result into:

.setOnSubmitCallback(function() {
    $("#searchResults").dialog( { minWidth: 750, minHeight: 750 } );
} );

So my problem now is, where and on what do I have to put the setOnSubmitCallback?

I cannot put it on google.search.Search or CustomSearchControl as it is stated in the documentation. ANd I cannot call it in the onLoadCallback so it is very strange for me. Cannt figure out how to do that.

I hope somebody has some more experience for the google search and could help me out with a solution.

Thank you very much in advance.

Answer

NOTE: the code below is using something Google deprecated. Use this instead: http://code.google.com/apis/customsearch/v1/overview.html

<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Hello World - Google  Web Search API Sample</title>
    <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/themes/ui-lightness/jquery-ui.css" type="text/css" media="all" />
    <script src="https://www.google.com/jsapi" type="text/javascript"></script>
    <script language="Javascript" type="text/javascript">
    //<![CDATA[
    google.load('search', '1');
    google.load("jquery", "1.5.2");
    google.load("jqueryui", "1.8.12");

    function OnLoad() {

        var searchComplete = function(searchControl, searcher){
            $('#searchResults').dialog({modal: true, width: 700, height: 400, position: [50, 50]});
            for (result in searcher.results) {
                var content = searcher.results[result].content;
                var title = searcher.results[result].title;
                var url = searcher.results[result].url;
                $('#searchResults ul')
                    .append($('<li></li>')
                        .append($('<a/>').attr('href', url).text(title))
                        .append($('<p/>').text(content)));
            }
        };

        // called on form submit
        newSearch = function(form) {
          if (form.input.value) {
            // Create a search control
            var searchControl = new google.search.SearchControl();

            // Add in a set of searchers
            searchControl.addSearcher(new google.search.WebSearch());
            searchControl.addSearcher(new google.search.VideoSearch());

            // tell the searchControl to draw itself (without this, the searchComplete won't get called - I'm not sure why)
            searchControl.draw();

            searchControl.setLinkTarget(google.search.Search.LINK_TARGET_SELF);           
            searchControl.setSearchCompleteCallback(this, searchComplete);
            searchControl.execute(form.input.value);
          }
          return false;
        }

        // create a search form without a clear button
        // bind form submission to my custom code
        var container = document.getElementById("searchFormContainer");
        this.searchForm = new google.search.SearchForm(false, container);
        this.searchForm.setOnSubmitCallback(this, newSearch);
    }
    google.setOnLoadCallback(OnLoad);

    //]]>
    </script>
  </head>
  <body>
    <div id="searchFormContainer">Loading</div>
    <div id="searchResults" title="Search Results">
        <ul></ul>
    </div>
  </body>
</html>
Categories
discuss

Java – Circular shift using bitwise operations

I am wondering how to implement a circular right shift by k of the bitstring represented by the int bits.

public int rtCircShift(int bits, int k)
{
    return bits >> k;
}

All this code does is return 0, how can I make it a circular shift?

Answer

This should work:

 return (bits >>> k) | (bits << (Integer.SIZE - k));

Also see the Wikipedia article on circular shifts.

Categories
discuss

Difference between RowId and Position in onItemLongClick’s parameter

I’ve got confuse on the 3rd and 4th parameter of onItemLongClick(…).
According to AdapterView.OnItemLongClickListener

position – The position of the view in the list

id – The row id of the item that was clicked

I couldn’t make any sense out from these, advice please.

Answer

position is the clicked element’s position in your Adapter (so you can do adapter.getItem(position) )

row id is the id that corresponds to that element, what your Adapter returns in the getItemId() method.

Categories
discuss

Android Maven build gives trouble processing “javax/xml/namespace/QName.class”:

Following error is showing in Maven console everytime I do Project -> Clean. Only the jar file is built in the target folder, apk file is not getting built. Can anyone please help me to solve this issue?

Regards,

Sam.

My development environment is;

  • Eclipse Helios
  • Android SDK API level 8
  • Eclipse ADT 10.0.1
  • Maven Integration for Android Development Tools 0.2.5

4/30/11 9:54:07 PM IST: [INFO] skip non existing resourceDirectory D:myworkspaceandroidandroid-test-appsrctestresources 4/30/11 9:54:07 PM IST: [INFO] Not compiling test sources 4/30/11 9:54:07 PM IST: [INFO] Tests are skipped. 4/30/11 9:54:07 PM IST: [INFO] D:myandroid-sdk-myandroid-sdk-windows/platform-tools/dx.bat [–dex, –output=D:myworkspaceandroidandroid-test-apptargetclasses.dex, D:myworkspaceandroidandroid-test-apptargetandroid-classes] 4/30/11 9:54:17 PM IST: [INFO] 4/30/11 9:54:17 PM IST: [INFO] trouble processing “javax/xml/namespace/QName.class”: 4/30/11 9:54:17 PM IST: [INFO] 4/30/11 9:54:17 PM IST: [INFO] Ill-advised or mistaken usage of a core class (java.* or javax.*) 4/30/11 9:54:17 PM IST: [INFO] when not building a core library. 4/30/11 9:54:17 PM IST: [INFO] 4/30/11 9:54:17 PM IST: [INFO] This is often due to inadvertently including a core library file 4/30/11 9:54:17 PM IST: [INFO] in your application’s project, when using an IDE (such as 4/30/11 9:54:17 PM IST: [INFO] Eclipse). If you are sure you’re not intentionally defining a 4/30/11 9:54:17 PM IST: [INFO] core class, then this is the most likely explanation of what’s 4/30/11 9:54:17 PM IST: [INFO] going on. 4/30/11 9:54:17 PM IST: [INFO] 4/30/11 9:54:17 PM IST: [INFO] However, you might actually be trying to define a class in a core 4/30/11 9:54:17 PM IST: [INFO] namespace, the source of which you may have taken, for example, 4/30/11 9:54:17 PM IST: [INFO] from a non-Android virtual machine project. This will most 4/30/11 9:54:17 PM IST: [INFO] assuredly not work. At a minimum, it jeopardizes the 4/30/11 9:54:17 PM IST: [INFO] compatibility of your app with future versions of the platform. 4/30/11 9:54:17 PM IST: [INFO] It is also often of questionable legality. 4/30/11 9:54:17 PM IST: [INFO] 4/30/11 9:54:17 PM IST: [INFO] If you really intend to build a core library — which is only 4/30/11 9:54:17 PM IST: [INFO] appropriate as part of creating a full virtual machine 4/30/11 9:54:17 PM IST: [INFO] distribution, as opposed to compiling an application — then use 4/30/11 9:54:17 PM IST: [INFO] the “–core-library” option to suppress this error message. 4/30/11 9:54:17 PM IST: [INFO] 4/30/11 9:54:17 PM IST: [INFO] If you go ahead and use “–core-library” but are in fact 4/30/11 9:54:17 PM IST: [INFO] building an application, then be forewarned that your application 4/30/11 9:54:17 PM IST: [INFO] will still fail to build or run, at some point. Please be 4/30/11 9:54:17 PM IST: [INFO] prepared for angry customers who find, for example, that your 4/30/11 9:54:17 PM IST: [INFO] application ceases to function once they upgrade their operating 4/30/11 9:54:17 PM IST: [INFO] system. You will be to blame for this problem. 4/30/11 9:54:17 PM IST: [INFO] 4/30/11 9:54:17 PM IST: [INFO] If you are legitimately using some code that happens to be in a 4/30/11 9:54:17 PM IST: [INFO] core package, then the easiest safe alternative you have is to 4/30/11 9:54:17 PM IST: [INFO] repackage that code. That is, move the classes in question into 4/30/11 9:54:17 PM IST: [INFO] your own package namespace. This means that they will never be in 4/30/11 9:54:17 PM IST: [INFO] conflict with core system classes. JarJar is a tool that may help 4/30/11 9:54:17 PM IST: [INFO] you in this endeavor. If you find that you cannot do this, then 4/30/11 9:54:17 PM IST: [INFO] that is an indication that the path you are on will ultimately 4/30/11 9:54:17 PM IST: [INFO] lead to pain, suffering, grief, and lamentation. 4/30/11 9:54:17 PM IST: [INFO] 4/30/11 9:54:17 PM IST: [INFO] 1 error; aborting

Answer

If your are using android maven integration, you should add the “provided” scope in your android sdk dependency.

<dependency>        
<groupId>com.google.android</groupId>        
<artifactId>android</artifactId>        
<version>2.3.3</version>        
<scope>provided</scope>        
</dependency>
Categories
discuss

AsyncSocket in java?

How can I implement an AsyncSocket in java (specificaly on android)?

Answer

If you want to do non blocking io in java without having 1 or more threads per socket, you have to use the java NIO libraries. There are some wrappers around NIO that make it easier to use, netty is a good example.

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..