Example usage for android.support.v4.widget SimpleCursorAdapter SimpleCursorAdapter

List of usage examples for android.support.v4.widget SimpleCursorAdapter SimpleCursorAdapter

Introduction

In this page you can find the example usage for android.support.v4.widget SimpleCursorAdapter SimpleCursorAdapter.

Prototype

public SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) 

Source Link

Document

Standard constructor.

Usage

From source file:com.wheelly.fragments.LocationsListFragment.java

private ListAdapter buildAdapter() {
    if (inSelectMode) {
        return new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_single_choice, null,
                new String[] { "name" }, new int[] { android.R.id.text1 }, 0) {
            {/*  w  w w  . j a  va 2s  .co  m*/
                setViewBinder(new ViewBinder() {
                    @Override
                    public boolean setViewValue(View view, Cursor cursor, int paramInt) {
                        if (android.R.id.text1 == view.getId()) {
                            final String argb = cursor.getString(cursor.getColumnIndex("color"));
                            if (!Strings.isNullOrEmpty(argb)) {
                                view.setBackgroundColor(Color.parseColor(argb));
                            }
                        }
                        return false;
                    }
                });
            }
        };
    }

    final SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.location_item, null,
            new String[] { "name", "resolved_address", "name" },
            new int[] { android.R.id.text1, android.R.id.text2, R.id.text3 }, 0);

    adapter.setViewBinder(new LocationViewBinder(null));

    LocationUtils.obtainLocation(getActivity(), new LocationListener() {
        @Override
        public void onLocationChanged(Location paramLocation) {
            ((LocationViewBinder) adapter.getViewBinder()).location = paramLocation;
            adapter.notifyDataSetChanged();
        }
    });

    return adapter;
}

From source file:com.josenaves.sunshine.app.ForecastFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // The SimpleCursorAdapter will take data from the database through the
    // Loader and use it to populate the ListView it's attached to.
    mForecastAdapter = new SimpleCursorAdapter(getActivity(), R.layout.list_item_forecast, null,
            // the column names to use to fill the textviews
            new String[] { WeatherContract.WeatherEntry.COLUMN_DATETEXT,
                    WeatherContract.WeatherEntry.COLUMN_SHORT_DESC,
                    WeatherContract.WeatherEntry.COLUMN_MAX_TEMP,
                    WeatherContract.WeatherEntry.COLUMN_MIN_TEMP },
            // the textviews to fill with the data pulled from the columns above
            new int[] { R.id.list_item_date_textview, R.id.list_item_forecast_textview,
                    R.id.list_item_high_textview, R.id.list_item_low_textview },
            0);/*from  w ww. ja va2 s. c  o  m*/

    mForecastAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            boolean isMetric = Utility.isMetric(getActivity());
            switch (columnIndex) {
            case COL_WEATHER_MAX_TEMP:
            case COL_WEATHER_MIN_TEMP: {
                // we have to do some formatting and possibly a conversion
                ((TextView) view).setText(Utility.formatTemperature(cursor.getDouble(columnIndex), isMetric));
                return true;
            }
            case COL_WEATHER_DATE: {
                String dateString = cursor.getString(columnIndex);
                TextView dateView = (TextView) view;
                dateView.setText(Utility.formatDate(dateString));
                return true;
            }
            }
            return false;
        }
    });

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    // Get a reference to the ListView, and attach this adapter to it.
    ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
    listView.setAdapter(mForecastAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            SimpleCursorAdapter adapter = (SimpleCursorAdapter) adapterView.getAdapter();
            Cursor cursor = adapter.getCursor();
            if (cursor != null && cursor.moveToPosition(position)) {
                String dateString = Utility.formatDate(cursor.getString(COL_WEATHER_DATE));
                String weatherDescription = cursor.getString(COL_WEATHER_DESC);

                boolean isMetric = Utility.isMetric(getActivity());
                String high = Utility.formatTemperature(cursor.getDouble(COL_WEATHER_MAX_TEMP), isMetric);
                String low = Utility.formatTemperature(cursor.getDouble(COL_WEATHER_MIN_TEMP), isMetric);

                String detailString = String.format("%s - %s - %s/%s", dateString, weatherDescription, high,
                        low);

                Intent intent = new Intent(getActivity(), DetailActivity.class).putExtra(Intent.EXTRA_TEXT,
                        detailString);
                startActivity(intent);
            }
        }
    });

    return rootView;
}

From source file:org.onebusaway.android.ui.MyRemindersFragment.java

private SimpleCursorAdapter newAdapter() {
    final String[] from = { ObaContract.Trips.NAME, ObaContract.Trips.HEADSIGN, ObaContract.Trips.DEPARTURE,
            ObaContract.Trips.ROUTE_ID };
    final int[] to = { R.id.name, R.id.headsign, R.id.departure_time, R.id.route_name };
    SimpleCursorAdapter simpleAdapter = new SimpleCursorAdapter(getActivity(), R.layout.trip_list_listitem,
            null, from, to, 0);//from  w w w. j  a  v a2 s.  c o m

    simpleAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            if (columnIndex == COL_NAME) {
                TextView text = (TextView) view;
                String name = cursor.getString(columnIndex);
                if (name.length() == 0) {
                    name = getString(R.string.trip_info_noname);
                }
                text.setText(name);
                return true;
            } else if (columnIndex == COL_HEADSIGN) {
                String headSign = cursor.getString(columnIndex);
                TextView text = (TextView) view;
                text.setText(MyTextUtils.toTitleCase(headSign));
                return true;
            } else if (columnIndex == COL_DEPARTURE) {
                TextView text = (TextView) view;
                text.setText(TripInfoActivity.getDepartureTime(getActivity(),
                        ObaContract.Trips.convertDBToTime(cursor.getInt(columnIndex))));
                return true;
            } else if (columnIndex == COL_ROUTE_ID) {
                //
                // Translate the Route ID into the Route Name by looking
                // it up in the Routes table.
                //
                TextView text = (TextView) view;
                final String routeId = cursor.getString(columnIndex);
                final String routeName = TripService.getRouteShortName(getActivity(), routeId);
                if (routeName != null) {
                    text.setText(getString(R.string.trip_info_route, routeName));
                }
                return true;
            }
            return false;
        }
    });
    return simpleAdapter;
}

From source file:com.bexkat.feedlib.ItemListFragment.java

@Override
public void onActivityCreated(Bundle saveInstanceState) {
    String[] from = new String[] { ItemTable.COLUMN_TITLE, ItemTable.COLUMN_PUBDATE, ItemTable.COLUMN_READ,
            ItemTable.COLUMN_FAVORITE };
    int[] to = new int[] { R.id.row_title, R.id.row_pubdate, R.id.row_status, R.id.row_status };

    super.onActivityCreated(saveInstanceState);

    getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override//from  www  .  ja va 2  s  .c  o m
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            if (mActionMode != null) {
                return false;
            }
            ItemTable db = new ItemTable(getSherlockActivity());
            selectedItem = db.getItem(id);
            // Start the CAB using the ActionMode.Callback defined above
            mActionMode = getSherlockActivity().startActionMode(mActionModeCallback);
            view.setSelected(true);
            return true;
        }
    });

    mCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.item_list, null, from, to,
            SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    mCursorAdapter.setViewBinder(new ViewBinder() {

        @Override
        public boolean setViewValue(View v, Cursor c, int index) {
            if (index == c.getColumnIndex(ItemTable.COLUMN_READ)) {
                ImageView iv = (ImageView) v;
                if (c.getInt(index) == DatabaseHelper.OFF)
                    iv.setImageResource(R.drawable.unread);
                return true;
            }
            if (index == c.getColumnIndex(ItemTable.COLUMN_PUBDATE)) {
                String date = mFormat.format(new Date(Long.parseLong(c.getString(index))));
                ((TextView) v).setText(date);
                return true;
            }
            if (index == c.getColumnIndex(ItemTable.COLUMN_FAVORITE)) {
                ImageView iv = (ImageView) v;
                if (c.getInt(index) == DatabaseHelper.ON)
                    iv.setImageResource(R.drawable.ic_favorite);
                if (c.getInt(c.getColumnIndex(ItemTable.COLUMN_READ)) == DatabaseHelper.ON
                        && c.getInt(index) == DatabaseHelper.OFF)
                    iv.setVisibility(View.INVISIBLE);
                else
                    iv.setVisibility(View.VISIBLE);
                return true;
            }
            return false;
        }

    });
    setListAdapter(mCursorAdapter);
    getLoaderManager().initLoader(0, null, this);
}

From source file:com.ffmpegtest.VideoActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFormat(PixelFormat.RGB_565);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DITHER);

    super.onCreate(savedInstanceState);

    this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    this.getWindow().setBackgroundDrawable(null);

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    if (sIsSurfaceView)
        this.setContentView(R.layout.video_surfaceview);
    else//from  w  w  w. ja va 2  s . c  o  m
        this.setContentView(R.layout.video_view);

    mSeekBar = (SeekBar) this.findViewById(R.id.seek_bar);
    mSeekBar.setOnSeekBarChangeListener(this);

    mPlayPauseButton = (Button) this.findViewById(R.id.play_pause);
    mPlayPauseButton.setOnClickListener(this);

    mScaleButton = this.findViewById(R.id.scale_type);
    mScaleButton.setOnClickListener(this);

    mControlsView = this.findViewById(R.id.controls);
    mStreamsView = this.findViewById(R.id.streams);
    mLoadingView = this.findViewById(R.id.loading_view);
    mLanguageSpinner = (Spinner) this.findViewById(R.id.language_spinner);
    mSubtitleSpinner = (Spinner) this.findViewById(R.id.subtitle_spinner);

    mLanguageAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, null, PROJECTION,
            new int[] { android.R.id.text1 }, 0);
    mLanguageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    mLanguageSpinner.setAdapter(mLanguageAdapter);
    mLanguageSpinner.setOnItemSelectedListener(this);

    mSubtitleAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, null, PROJECTION,
            new int[] { android.R.id.text1 }, 0);
    mSubtitleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    mSubtitleSpinner.setAdapter(mSubtitleAdapter);
    mSubtitleSpinner.setOnItemSelectedListener(this);

    mVideoView = this.findViewById(R.id.video_view);
    ((FFmpegSurfaceView) mVideoView).setScaleType(ScaleType.CENTER_INSIDE, false);
    mMpegPlayer = new FFmpegPlayer((FFmpegDisplay) mVideoView, this);
    mMpegPlayer.setMpegListener(this);
    setDataSource();
}

From source file:com.yanzhenjie.searchview.sample.SearchActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_action_user_search));
    Utils.setSearchViewCursorColor(mSearchView, R.drawable.text_cursor_white);
    mSearchView.setSubmitButtonEnabled(true);
    mSearchView.setQueryHint(getText(R.string.title_search_user_hint));
    mSearchView.setOnQueryTextListener(mOnQueryTextListener);

    // Can not exit to icon.
    // mSearchView.setOnCloseListener(() -> TextUtils.isEmpty(mSearchView.getQuery()));

    // Automatic expansion.
    //        mSearchView.setIconified(false);
    //        mSearchView.setIconifiedByDefault(true);

    mSearchView.setOnSuggestionListener(mSuggestionListener);
    SearchableInfo searchableInfo = ((SearchManager) getSystemService(Context.SEARCH_SERVICE))
            .getSearchableInfo(getComponentName());
    mSearchView.setSearchableInfo(searchableInfo);

    mCursorAdapter = new SimpleCursorAdapter(this, R.layout.item_text_list_popup, null,
            new String[] { UserSearchProvider.ProviderInfo.USER_NAME,
                    UserSearchProvider.ProviderInfo.USER_DES },
            new int[] { R.id.tv_item_list, R.id.tv_item_list_des },
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    mSearchView.setSuggestionsAdapter(mCursorAdapter);

    getSupportLoaderManager().initLoader(CURSOR_LOADER_ID, null, mLoaderCallbacks);
    return true;//from  w  w w . j ava 2s. co m
}

From source file:pl.itiner.grave.ResultList.java

private SimpleCursorAdapter createAdapter() {
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.list_item, null,
            new String[] { COLUMN_NAME, COLUMN_SURENAME, COLUMN_CEMENTERY_ID, COLUMN_DATE_BIRTH,
                    COLUMN_DATE_DEATH, COLUMN_DATE_BURIAL },
            new int[] { R.id.list_value_name, R.id.list_value_surname, R.id.list_value_cementry,
                    R.id.list_value_dateBirth, R.id.list_value_dateDeath, R.id.list_value_dateBurial },
            SimpleCursorAdapter.NO_SELECTION);
    adapter.setViewBinder(new ResultList.ResultListViewBinder());
    return adapter;
}

From source file:com.jsw.MngProductDatabase.Fragments.ManageProduct_Fragment.java

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    if (oldProduct != null) {
        ;/*w  w w .j a v  a 2 s .c om*/
        mName.getEditText().setText(oldProduct.getName());
        mTrademark.getEditText().setText(oldProduct.getBrand());
        mDosage.getEditText().setText(oldProduct.getDosage());
        mStock.getEditText().setText(oldProduct.getStock());
        mPrice.getEditText().setText(String.valueOf(oldProduct.getPrice()));
        mUrl.getEditText().setText(oldProduct.getImage());
        mCategory.setSelection(0);
        mDescription.getEditText().setText(oldProduct.getDescription());
    }

    mFabSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            save();
        }
    });

    String[] from = { Contract.CategoryEntry.COLUMN_NAME };
    int[] to = { android.R.id.text1 };

    mCursorAdapter = new SimpleCursorAdapter(getContext(), android.R.layout.simple_spinner_item, null, from, to,
            0);
    mCursorAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
    mCategory.setAdapter(mCursorAdapter);

}

From source file:com.kyakujin.android.tagnotepad.ui.TagDialogListFragment.java

/**
 * ????????// w  ww  . j a  v a 2 s  . c  o  m
 */
private void fillTagList() {
    mTagListAdapter = new SimpleCursorAdapter(getActivity(), R.layout.checkedtextview, null,
            new String[] { Tags.TAGNAME }, new int[] { R.id.checkedtextview }, 0);

    mTagListView.setAdapter(mTagListAdapter);
    mTagListView.setItemsCanFocus(false);
    mTagListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    mManager.restartLoader(TagsQuery.LOADER_ID, null, this);
}

From source file:org.openbmap.activities.SessionListFragment.java

/**
 * @link http://stackoverflow.com/questions/6317767/cant-add-a-headerview-to-a-listfragment
 *      Fragment lifecycle//from w w w  .j  av  a 2  s  .  c o m
 *      onAttach(Activity) called once the fragment is associated with its activity.
 *      onCreate(Bundle) called to do initial creation of the fragment.
 *      onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
 *      onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreate.
 *      onStart() makes the fragment visible to the user (based on its containing activity being started).
 *      onResume() makes the fragment interacting with the user (based on its containing activity being resumed).
 */

@Override
public final void onActivityCreated(final Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    final String[] from = new String[] { Schema.COL_ID, Schema.COL_CREATED_AT, Schema.COL_IS_ACTIVE,
            Schema.COL_HAS_BEEN_EXPORTED, Schema.COL_NUMBER_OF_CELLS, Schema.COL_NUMBER_OF_WIFIS };

    final int[] to = new int[] { R.id.sessionlistfragment_id, R.id.sessionlistfragment_created_at,
            R.id.sessionlistfragment_statusicon, R.id.sessionlistfragment_uploadicon,
            R.id.sessionlistfragment_no_cells, R.id.sessionlistfragment_no_wifis };

    mAdapter = new SimpleCursorAdapter(getActivity().getBaseContext(), R.layout.sessionlistfragment, null, from,
            to, 0);
    mAdapter.setViewBinder(new SessionViewBinder());

    // Trying to add a Header View.
    final View header = (View) getLayoutInflater(savedInstanceState).inflate(R.layout.sessionlistheader, null);
    this.getListView().addHeaderView(header);

    // setup data adapters
    setListAdapter(mAdapter);
    getActivity().getSupportLoaderManager().initLoader(0, null, this);

    // register for change notifications
    mObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(final boolean selfChange) {
            refreshAdapter();
        };
    };

    getListView().setLongClickable(true);
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    getListView().setOnItemLongClickListener(new ActionModeUtils((ActionBarActivity) this.getActivity(),
            R.menu.session_context, this, getListView()));
}