Android Open Source - campus Search Suggestion Adapter






From Project

Back to project page campus.

License

The source code is released under:

GNU General Public License

If you think the Android project campus listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package lecho.app.campus.adapter;
/*from   w  ww.j a v  a 2  s  .c o m*/
import lecho.app.campus.R;
import lecho.app.campus.dao.PlaceDao;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.widget.ResourceCursorAdapter;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.actionbarsherlock.widget.SearchView;

public class SearchSuggestionAdapter extends ResourceCursorAdapter {
  private static final String TAG = "SearchSuggestionAdapter";
  private SearchableInfo mSearchableInfo;
  private SearchView mSearchView;

  static final int INVALID_INDEX = -1;
  // Cached column indexes, updated when the cursor changes.
  private int mSymbolCol = INVALID_INDEX;
  private int mNameCol = INVALID_INDEX;
  private int mDescriptionCol = INVALID_INDEX;

  public SearchSuggestionAdapter(Context context, SearchView searchView, SearchableInfo searchableInfo) {
    super(context, R.layout.search_dropdown_item, null, FLAG_REGISTER_CONTENT_OBSERVER);
    mSearchableInfo = searchableInfo;
    mSearchView = searchView;
  }

  /**
   * Tags the view with cached child view look-ups.
   */
  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View v = super.newView(context, cursor, parent);
    v.setTag(new ChildViewCache(v));
    return v;
  }

  @Override
  public void bindView(View view, Context context, Cursor cursor) {
    if (mSymbolCol < 0 | mNameCol < 0 | mDescriptionCol < 0) {
      Log.w(TAG, "Bind viw called with invalid column indexes");
      return;
    }
    ChildViewCache views = (ChildViewCache) view.getTag();

    String symbol = cursor.getString(mSymbolCol);
    String name = cursor.getString(mNameCol);
    String description = cursor.getString(mDescriptionCol);

    views.mSymbol.setText(symbol);

    if (TextUtils.isEmpty(description)) {
      views.mName.setSingleLine(false);
      views.mName.setMaxLines(2);
      views.mName.setText(name);
      views.mDescription.setVisibility(View.GONE);
    } else {
      views.mName.setSingleLine(true);
      views.mName.setMaxLines(1);
      views.mName.setText(name);
      views.mDescription.setVisibility(View.VISIBLE);
      views.mDescription.setText(description);
    }
  }

  @Override
  public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
    String query = (constraint == null) ? "" : constraint.toString();
    /**
     * for in app search we show the progress spinner until the cursor is
     * returned with the results.
     */
    Cursor cursor = null;
    if (mSearchView.getVisibility() != View.VISIBLE || mSearchView.getWindowVisibility() != View.VISIBLE) {
      return null;
    }
    // mSearchView.getWindow().getDecorView().post(mStartSpinnerRunnable);
    // // TODO:
    try {
      cursor = getSuggestions(query);
      // trigger fill window so the spinner stays up until the results
      // are copied over and
      // closer to being ready
      if (cursor != null) {
        return cursor;
      }
    } catch (RuntimeException e) {
      Log.w(TAG, "Search suggestions query threw an exception.", e);
    }
    // If cursor is null or an exception was thrown, stop the spinner
    // and return null.
    // changeCursor doesn't get called if cursor is null
    // mSearchView.getWindow().getDecorView().post(mStopSpinnerRunnable);
    return null;
  }

  public Cursor getSuggestions(String query) {
    if (mSearchableInfo == null) {
      return null;
    }

    String authority = mSearchableInfo.getSuggestAuthority();
    if (authority == null) {
      return null;
    }

    Uri.Builder uriBuilder = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority)
        .query("") // TODO: Remove, workaround for a bug in
              // Uri.writeToParcel()
        .fragment(""); // TODO: Remove, workaround for a bug in
                // Uri.writeToParcel()

    // if content path provided, insert it now
    final String contentPath = mSearchableInfo.getSuggestPath();
    if (contentPath != null) {
      uriBuilder.appendEncodedPath(contentPath);
    }

    // append standard suggestion query path
    uriBuilder.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY);

    // get the query selection, may be null
    String selection = mSearchableInfo.getSuggestSelection();
    // inject query, either as selection args or inline
    String[] selArgs = null;
    if (selection != null) { // use selection if provided
      selArgs = new String[] { query };
    } else { // no selection, use REST pattern
      uriBuilder.appendPath(query);
    }

    Uri uri = uriBuilder.build();

    // finally, make the query
    return mContext.getContentResolver().query(uri, null, selection, selArgs, null);
  }

  /**
   * Cache columns.
   */
  @Override
  public void changeCursor(Cursor c) {
    try {
      super.changeCursor(c);

      if (c != null) {
        mSymbolCol = c.getColumnIndex(PlaceDao.Properties.Symbol.columnName);
        mNameCol = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1);
        mDescriptionCol = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2);
      }
    } catch (Exception e) {
      Log.e(TAG, "error changing cursor and caching columns", e);
    }
  }

  /**
   * Cache of the child views of drop-drown list items, to avoid looking up
   * the children each time the contents of a list item are changed.
   */
  private final static class ChildViewCache {
    public final TextView mSymbol;
    public final TextView mName;
    public final TextView mDescription;

    public ChildViewCache(View v) {
      mSymbol = (TextView) v.findViewById(R.id.symbol);
      mName = (TextView) v.findViewById(R.id.name);
      mDescription = (TextView) v.findViewById(R.id.description);
    }
  }
}




Java Source Code List

lecho.app.campus.activity.AboutAppActivity.java
lecho.app.campus.activity.CampusMapActivity.java
lecho.app.campus.activity.GalleryActivity.java
lecho.app.campus.activity.LegalInfoActivity.java
lecho.app.campus.activity.PlaceDetailsActivity.java
lecho.app.campus.adapter.GalleryPagesFragmentAdapter.java
lecho.app.campus.adapter.MarkerInfoWindowAdapter.java
lecho.app.campus.adapter.NavigationDrawerAdapter.java
lecho.app.campus.adapter.PlaceDetailsFragmentAdapter.java
lecho.app.campus.adapter.SearchResultViewAdapter.java
lecho.app.campus.adapter.SearchSuggestionAdapter.java
lecho.app.campus.dao.CategoryDao.java
lecho.app.campus.dao.CategoryDao.java
lecho.app.campus.dao.Category.java
lecho.app.campus.dao.Category.java
lecho.app.campus.dao.DaoMaster.java
lecho.app.campus.dao.DaoMaster.java
lecho.app.campus.dao.DaoSession.java
lecho.app.campus.dao.DaoSession.java
lecho.app.campus.dao.FacultyDao.java
lecho.app.campus.dao.FacultyDao.java
lecho.app.campus.dao.Faculty.java
lecho.app.campus.dao.Faculty.java
lecho.app.campus.dao.Main.java
lecho.app.campus.dao.PlaceCategoryDao.java
lecho.app.campus.dao.PlaceCategoryDao.java
lecho.app.campus.dao.PlaceCategory.java
lecho.app.campus.dao.PlaceCategory.java
lecho.app.campus.dao.PlaceDao.java
lecho.app.campus.dao.PlaceDao.java
lecho.app.campus.dao.PlaceFacultyDao.java
lecho.app.campus.dao.PlaceFacultyDao.java
lecho.app.campus.dao.PlaceFaculty.java
lecho.app.campus.dao.PlaceFaculty.java
lecho.app.campus.dao.PlaceUnitDao.java
lecho.app.campus.dao.PlaceUnitDao.java
lecho.app.campus.dao.PlaceUnit.java
lecho.app.campus.dao.PlaceUnit.java
lecho.app.campus.dao.Place.java
lecho.app.campus.dao.Place.java
lecho.app.campus.dao.UnitDao.java
lecho.app.campus.dao.UnitDao.java
lecho.app.campus.dao.Unit.java
lecho.app.campus.dao.Unit.java
lecho.app.campus.fragment.GalleryPageFragment.java
lecho.app.campus.fragment.PlaceDetailsFragment.java
lecho.app.campus.fragment.dialog.NoInternetConnectionDialogFragment.java
lecho.app.campus.fragment.dialog.PlayServicesErrorDialogFragment.java
lecho.app.campus.loader.PlaceDetailsLoader.java
lecho.app.campus.loader.PlacesLoader.java
lecho.app.campus.provider.SearchSuggestionProvider.java
lecho.app.campus.service.PopulateDBIntentService.java
lecho.app.campus.utils.ABSMenuItemConverter.java
lecho.app.campus.utils.BitmapAsyncTask.java
lecho.app.campus.utils.Config.java
lecho.app.campus.utils.DataParser.java
lecho.app.campus.utils.DatabaseHelper.java
lecho.app.campus.utils.ImagesDirAsyncTask.java
lecho.app.campus.utils.NavigationDrawerItem.java
lecho.app.campus.utils.PlaceDetails.java
lecho.app.campus.utils.PlacesList.java
lecho.app.campus.utils.UnitsGroup.java
lecho.app.campus.utils.Utils.java
lecho.app.campus.view.CheckableDrawerItem.java
lecho.app.campus.view.UnitsGroupLayout.java
lecho.app.campus.view.ZoomImageView.java
lecho.app.campus.view.ZoomViewPager.java