android coding patterns


public Cursor query(Uri uri, String[] projection, 
          String selection, String[] selectionArgs,
          String sortOrder) 
{
   if (!TextUtils.isEmpty(selection)) {
      throw new IllegalArgumentException(
          "selection not allowed for " + uri);
   }
   if (selectionArgs != null && selectionArgs.length != 0) {
      throw new IllegalArgumentException(
          "selectionArgs not allowed for " + uri);
   }
   if (!TextUtils.isEmpty(sortOrder)) {
      throw new IllegalArgumentException(
          "sortOrder not allowed for " + uri);
   }
....
}

private static final UriMatcher sURIMatcher = initializeUriMatcher();

private static final int SEARCH_SUGGEST = 0;
private static final int SHORTCUT_REFRESH = 1;

private static UriMatcher initializeUriMatcher() {
   UriMatcher matcher =  new UriMatcher(UriMatcher.NO_MATCH);
   matcher.addURI(AUTHORITY, 
        SearchManager.SUGGEST_URI_PATH_QUERY, 
        SEARCH_SUGGEST);
       
   matcher.addURI(AUTHORITY, 
        SearchManager.SUGGEST_URI_PATH_QUERY + "/*", 
        SEARCH_SUGGEST);
       
   matcher.addURI(AUTHORITY, 
        SearchManager.SUGGEST_URI_PATH_SHORTCUT, 
        SHORTCUT_REFRESH);
       
   matcher.addURI(AUTHORITY, 
        SearchManager.SUGGEST_URI_PATH_SHORTCUT + "/*", 
        SHORTCUT_REFRESH);
       
   return matcher;
}

switch (sURIMatcher.match(uri)) 
{
   case SEARCH_SUGGEST:
      //deal with uri type 1
      .....
   case SEARCH_SUGGEST:
      //deal with uri type 2
      .....
   default:
      .....
}

ctl-shift-o
or
rightclick/source/organize imports

List<type> mylist;
for (type item : mylift)
{
   item.toString();
}

public Uri insert(Uri uri, ContentValues values) {
        throw new UnsupportedOperationException();
    }

public String getType(Uri uri) {
        switch (sURIMatcher.match(uri)) {
            case SEARCH_SUGGEST:
                return SearchManager.SUGGEST_MIME_TYPE;
            case SHORTCUT_REFRESH:
                return SearchManager.SHORTCUT_MIME_TYPE;
            default:
                throw new IllegalArgumentException("Unknown URL " + uri);
        }
    }

private Object[] columnValuesOfWord(Dictionary.Word word) {
   return new String[] {
         word.word,           // _id
         word.word,           // text1
         word.definition,     // text2
         word.word,           // intent_data
   };

Especially when you are in XML files, eclipse may not prompt you for code completion by default. Just press CTRL-space to tell eclipse to prompt you. Try it. You won't be disappointed.

CTL-SHIFT-O: fix your imports

The location that R.java gets created is based on the java package name that is specified in the manifest file.

Implication is that if you were to refactor your java package name, it may not change the package name in the manifest file.

Fix this package name in the manifest to reflect your refactored java package names.


<manifest 
   xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.ai.android.search.simplesp"
...
/>

//res/menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- This group uses the default category. -->
    <group android:id="@+id/menuGroup_Main">
        <item android:id="@+id/mid_reset_suggestions"
            android:title="Reset Suggestions" />
    </group>
</menu>

//inflate the menu in the activity

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
//call the parent to attach any system level menus
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater(); //from activity
inflater.inflate(R.menu.reset_suggestions_menu, menu);
return true;
}

//Respond to it

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
    	if (item.getItemId() == R.id.mid_reset_suggestions)
    	{
            SearchRecentSuggestions suggestions = 
            	new SearchRecentSuggestions(this, 
            		SimpleSuggestionProvider.AUTHORITY, 
            		SimpleSuggestionProvider.MODE);
            suggestions.clearHistory();
    	}
    	return true;
    }

SearchRecentSuggestions suggestions = 
      	new SearchRecentSuggestions(this, 
      		SimpleSuggestionProvider.AUTHORITY, 
       		SimpleSuggestionProvider.MODE);
suggestions.clearHistory();