Example usage for android.widget AdapterView getItemAtPosition

List of usage examples for android.widget AdapterView getItemAtPosition

Introduction

In this page you can find the example usage for android.widget AdapterView getItemAtPosition.

Prototype

public Object getItemAtPosition(int position) 

Source Link

Document

Gets the data associated with the specified position in the list.

Usage

From source file:net.inbox.InboxUI.java

public void populate_list_view() {
    ArrayList<InboxMessageListItem> al_messages_items;
    ArrayList<Message> al_messages = db.get_all_messages(current.get_id());

    if (al_messages.size() == 0) {
        tv_no_account.setVisibility(View.VISIBLE);
        msg_list_view.setVisibility(View.GONE);
    } else {//from   w ww. j  av  a  2  s.  co  m
        tv_no_account.setVisibility(View.GONE);
        msg_list_view.setVisibility(View.VISIBLE);

        al_messages_items = new ArrayList<>();
        // Adding messages backwards, to save on sort by date
        for (int i = al_messages.size() - 1; i >= 0; --i) {
            Message nfo = al_messages.get(i);
            al_messages_items.add(new InboxMessageListItem(nfo.get_id(), nfo.get_account(), nfo.get_subject(),
                    nfo.get_from(), nfo.get_attachments(), nfo.get_seen()));
        }
        InboxMessageList msg_list_adapter = new InboxMessageList(this, al_messages_items);
        msg_list_view.setAdapter(msg_list_adapter);
        msg_list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                InboxMessageListItem itm_new = (InboxMessageListItem) parent.getItemAtPosition(position);
                if (!itm_new.get_seen()) {
                    msg_item_unseen = true;
                    change_unseen = true;
                }
                Intent i = new Intent(getApplicationContext(), InboxMessage.class);
                Bundle b = new Bundle();
                b.putInt("db_id", itm_new.get_id());
                b.putInt("db_inbox", itm_new.get_inbox());
                b.putString("title", current.get_email());
                b.putBoolean("no_send", current.get_smtp_server().isEmpty());
                b.putBoolean("imap_or_pop", current.get_imap_or_pop());
                startActivityForResult(i.putExtras(b), 1000);
                overridePendingTransition(R.anim.left_in, R.anim.left_out);
            }
        });
    }

    // Update message counter
    set_count();

    // Set server certificate details
    connection_security();
}

From source file:com.cattle.fragments.UserPhotosFragment.java

public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
    MediaStoreBucket item = (MediaStoreBucket) adapterView.getItemAtPosition(position);
    if (null != item) {
        loadBucketId(item.getId());//w ww. j  ava  2  s.c om
    }
}

From source file:me.diskstation.ammon.gpsrunner.ui.CalendarFragment.java

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    if (!selectionMode) {
        //Call DetailsTabActivity with runIds = id;
        Intent intent = new Intent(getActivity(), DetailsTabActivity.class);
        Run selectedRun = (Run) parent.getItemAtPosition(position);
        //Run testRun = (Run) parent.getSelectedItem();
        long[] runIds = { selectedRun.id };
        intent.putExtra("run_ids", runIds);
        startActivity(intent);// ww w  . j  a  v a 2 s .c  o  m
    }
}

From source file:com.example.android.wizardpager.wizard.ui.ProfessorCityToFragment.java

private void setupSpinners() {

    if (spnState.getAdapter() == null) {
        String[] states = getResources().getStringArray(R.array.states);
        for (int i = 0; i < states.length; i++) {
            mStates.add(new State(i, states[i]));
        }/*  w  w w.  jav a2s. c  o  m*/
        spnState.setAdapter(new StateSpinnerBaseAdapter(getActivity(), mStates));
    }

    spnState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            State selectedState = (State) parent.getItemAtPosition(position);
            if (position != stateSelectedPosition && selectedState.getId() != 0) {

                showDialog(getString(R.string.please_wait), getString(R.string.main_loading_cities));
                // Remove localities
                resetSpinner(spnLocality);
                mCities.clear();
                mTowns.clear();
                stateSelectedPosition = position;
                citySelectedPosition = 0;
                townSelectedPosition = 0;
                mPage.getData().putParcelable(ProfessorCityToPage.STATE_TO_DATA_KEY, selectedState);
                mPage.getData().remove(ProfessorCityToPage.MUNICIPALITY_TO_DATA_KEY);
                mPage.getData().remove(ProfessorCityToPage.LOCALITY_TO_DATA_KEY);
                mPage.notifyDataChanged();

                try {
                    InegiFacilRestClient.get().getCities(String.valueOf(selectedState.getId()),
                            new Callback<ArrayList<City>>() {
                                @Override
                                public void success(ArrayList<City> cities, Response response) {
                                    mCities = cities;
                                    spnMunicipality
                                            .setAdapter(new CitySpinnerBaseAdapter(getActivity(), cities));
                                    hideDialog();
                                }

                                @Override
                                public void failure(RetrofitError error) {
                                    hideDialog();
                                }
                            });

                } catch (Exception ex) {
                    Log.d("An error ocurred", ex.getMessage());
                }
            } else {
                if (selectedState.getId() == 0) {
                    resetSpinner(spnMunicipality);
                    resetSpinner(spnLocality);
                }
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

    spnMunicipality.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            City selectedCity = (City) parent.getItemAtPosition(position);
            if (citySelectedPosition != position && position != 0 && getUserVisibleHint()) {

                showDialog(getString(R.string.please_wait), getString(R.string.main_loading_localities));
                mTowns.clear();
                citySelectedPosition = position;
                townSelectedPosition = 0;
                mPage.getData().putParcelable(ProfessorCityToPage.MUNICIPALITY_TO_DATA_KEY, selectedCity);
                mPage.getData().remove(ProfessorCityToPage.LOCALITY_TO_DATA_KEY);
                mPage.notifyDataChanged();

                try {
                    InegiFacilRestClient.get().getTowns(String.valueOf(selectedCity.getClaveEntidad()),
                            String.valueOf(selectedCity.getClaveMunicipio()), new Callback<ArrayList<Town>>() {
                                @Override
                                public void success(ArrayList<Town> towns, Response response) {
                                    mTowns = towns;
                                    spnLocality.setAdapter(new TownSpinnerBaseAdapter(getActivity(), towns));
                                    hideDialog();
                                }

                                @Override
                                public void failure(RetrofitError error) {
                                    hideDialog();
                                }
                            });

                } catch (Exception ex) {
                    Log.d("An error occurred", ex.getMessage());
                }
            } else {
                if (position != 0) {
                    resetSpinner(spnLocality);
                }
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

    spnLocality.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            Town town = (Town) parent.getItemAtPosition(position);
            townSelectedPosition = position;
            mPage.getData().putParcelable(ProfessorCityToPage.LOCALITY_TO_DATA_KEY, town);
            mPage.notifyDataChanged();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

From source file:com.github.pockethub.android.ui.gist.GistFragment.java

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Object item = parent.getItemAtPosition(position);
    if (item instanceof GistFile) {
        startActivity(GistFilesViewActivity.createIntent(gist, position - 1));
    }//from  w  w  w . j  a v  a  2 s . co  m
}

From source file:ua.com.elius.sunshine.app.ForecastFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (savedInstanceState != null && savedInstanceState.containsKey(SELECTED_KEY)) {
        mPosition = savedInstanceState.getInt(SELECTED_KEY);
    }//w w  w  . j a  va 2 s  .c  om

    // The CursorAdapter will take data from our cursor and populate the ListView.
    mForecastAdapter = new ForecastAdapter(getActivity(), null, 0);

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

    // Get a reference to the ListView, and attach this adapter to it.
    mListView = (ListView) rootView.findViewById(R.id.listview_forecast);
    mListView.setAdapter(mForecastAdapter);

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView adapterView, View view, int position, long l) {
            // CursorAdapter returns a cursor at the correct position for getItem(), or null
            // if it cannot seek to that position.
            Cursor cursor = (Cursor) adapterView.getItemAtPosition(position);
            if (cursor != null) {
                String locationSetting = Utility.getPreferredLocation(getActivity());
                Callback cb;
                try {
                    cb = (Callback) getActivity();
                    Uri dateUri = WeatherContract.WeatherEntry.buildWeatherLocationWithDate(locationSetting,
                            cursor.getLong(COL_WEATHER_DATE));
                    cb.onItemSelected(dateUri);
                } catch (ClassCastException e) {
                    Log.e(LOG_TAG, "Activity does not implement required callback");
                }
                mPosition = position;
            }
        }
    });

    return rootView;
}

From source file:com.djkong.android.greatweather.ForecastFragment.java

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

    // The ForecastAdapter will take data from a source and
    // use it to populate the ListView it's attached to.
    mForecastAdapter = new ForecastAdapter(getActivity(), null, 0);

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

    // Get a reference to the ListView, and attach this adapter to it.
    mListView = (ListView) rootView.findViewById(R.id.listview_forecast);
    mListView.setBackgroundColor(Color.LTGRAY);
    mListView.setAdapter(mForecastAdapter);
    // We'll call our MainActivity
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override/* w w  w.  j  a  v  a 2  s  . c o m*/
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            // CursorAdapter returns a cursor at the correct position for getItem(), or null
            // if it cannot seek to that position.
            Cursor cursor = (Cursor) adapterView.getItemAtPosition(position);
            if (cursor != null) {
                String locationSetting = Utility.getPreferredLocation(getActivity());
                ((Callback) getActivity()).onItemSelected(WeatherContract.WeatherEntry
                        .buildWeatherLocationWithDate(locationSetting, cursor.getLong(COL_WEATHER_DATE)));
            }
            mPosition = position;
        }
    });

    // If there's instance state, mine it for useful information.
    // The end-goal here is that the user never knows that turning their device sideways
    // does crazy lifecycle related things.  It should feel like some stuff stretched out,
    // or magically appeared to take advantage of room, but data or place in the app was never
    // actually *lost*.
    if (savedInstanceState != null && savedInstanceState.containsKey(SELECTED_KEY)) {
        // The listview probably hasn't even been populated yet.  Actually perform the
        // swapout in onLoadFinished.
        mPosition = savedInstanceState.getInt(SELECTED_KEY);
    }

    mForecastAdapter.setUseTodayLayout(mUseTodayLayout);

    return rootView;
}

From source file:com.example.android.wizardpager.wizard.ui.ProfessorCityFromFragment.java

private void setupSpinners() {

    if (spnState.getAdapter() == null) {
        String[] states = getResources().getStringArray(R.array.states);
        for (int i = 0; i < states.length; i++) {
            mStates.add(new State(i, states[i]));
        }/*  w  w w .  j av  a  2  s .c  om*/
        spnState.setAdapter(new StateSpinnerBaseAdapter(getActivity(), mStates));
    }

    spnState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            State selectedState = (State) parent.getItemAtPosition(position);
            if (position != stateSelectedPosition && selectedState.getId() != 0) {

                showDialog(getString(R.string.please_wait), getString(R.string.main_loading_cities));
                // Remove localities
                resetSpinner(spnLocality);
                mCities.clear();
                mTowns.clear();
                stateSelectedPosition = position;
                citySelectedPosition = 0;
                townSelectedPosition = 0;
                mPage.getData().putParcelable(ProfessorCityFromPage.STATE_DATA_KEY, selectedState);
                mPage.getData().remove(ProfessorCityFromPage.MUNICIPALITY_DATA_KEY);
                mPage.getData().remove(ProfessorCityFromPage.LOCALITY_DATA_KEY);
                mPage.notifyDataChanged();

                try {
                    InegiFacilRestClient.get().getCities(String.valueOf(selectedState.getId()),
                            new Callback<ArrayList<City>>() {
                                @Override
                                public void success(ArrayList<City> cities, Response response) {
                                    mCities = cities;
                                    spnMunicipality
                                            .setAdapter(new CitySpinnerBaseAdapter(getActivity(), cities));
                                    hideDialog();
                                }

                                @Override
                                public void failure(RetrofitError error) {
                                    hideDialog();
                                }
                            });

                } catch (Exception ex) {
                    Log.d("An error ocurred", ex.getMessage());
                }
            } else {
                if (selectedState.getId() == 0) {
                    resetSpinner(spnMunicipality);
                    resetSpinner(spnLocality);
                }
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

    spnMunicipality.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            City selectedCity = (City) parent.getItemAtPosition(position);
            if (citySelectedPosition != position && position != 0 && getUserVisibleHint()) {

                showDialog(getString(R.string.please_wait), getString(R.string.main_loading_localities));
                mTowns.clear();
                citySelectedPosition = position;
                townSelectedPosition = 0;
                mPage.getData().putParcelable(ProfessorCityFromPage.MUNICIPALITY_DATA_KEY, selectedCity);
                mPage.getData().remove(ProfessorCityFromPage.LOCALITY_DATA_KEY);
                mPage.notifyDataChanged();

                try {
                    InegiFacilRestClient.get().getTowns(String.valueOf(selectedCity.getClaveEntidad()),
                            String.valueOf(selectedCity.getClaveMunicipio()), new Callback<ArrayList<Town>>() {
                                @Override
                                public void success(ArrayList<Town> towns, Response response) {
                                    mTowns = towns;
                                    spnLocality.setAdapter(new TownSpinnerBaseAdapter(getActivity(), towns));
                                    hideDialog();
                                }

                                @Override
                                public void failure(RetrofitError error) {
                                    hideDialog();
                                }
                            });

                } catch (Exception ex) {
                    Log.d("An error occurred", ex.getMessage());
                }
            } else {
                if (position != 0) {
                    resetSpinner(spnLocality);
                }
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

    spnLocality.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            Town town = (Town) parent.getItemAtPosition(position);
            townSelectedPosition = position;
            mPage.getData().putParcelable(ProfessorCityFromPage.LOCALITY_DATA_KEY, town);
            mPage.notifyDataChanged();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

From source file:es.usc.citius.servando.calendula.activities.MedicinesActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_medicines);
    color = DB.patients().getActive(this).color();
    setupToolbar(null, color);// w  ww. ja v a2  s. c o  m
    setupStatusBar(color);

    processIntent();

    TextView title = ((TextView) findViewById(R.id.textView2));

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    searchView = findViewById(R.id.search_view);
    closeSearchButton = findViewById(R.id.close_search_button);
    addButton = (FloatingActionButton) findViewById(R.id.add_button);
    searchEditText = (EditText) findViewById(R.id.search_edit_text);
    searchList = (ListView) findViewById(R.id.search_list);
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((MedicineCreateOrEditFragment) getViewPagerFragment(0)).onEdit();
        }
    });

    closeSearchButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            hideSearchView();
        }
    });

    adapter = new AutoCompleteAdapter(this, R.layout.med_drop_down_item);
    searchList.setAdapter(adapter);

    searchEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String filter = searchEditText.getText().toString();
            adapter.getFilter().filter(filter);
        }
    });

    searchList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Prescription p = (Prescription) parent.getItemAtPosition(position);
            hideSearchView();
            ((MedicineCreateOrEditFragment) getViewPagerFragment(0)).setPrescription(p);
        }
    });

    title.setBackgroundColor(color);
    searchView.setBackgroundColor(color);

    hideSearchView();
}

From source file:com.example.android.petsdb.EditorActivity.java

/**
 * Setup the dropdown spinner that allows the user to select the gender of the pet.
 *///from  ww  w.  ja  v  a  2s  .c  om
private void setupSpinner() {
    // Create adapter for spinner. The list options are from the String array it will use
    // the spinner will use the default layout
    ArrayAdapter genderSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.array_gender_options,
            android.R.layout.simple_spinner_item);

    // Specify dropdown layout style - simple list view with 1 item per line
    genderSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

    // Apply the adapter to the spinner
    mGenderSpinner.setAdapter(genderSpinnerAdapter);

    // Set the integer mSelected to the constant values
    mGenderSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String selection = (String) parent.getItemAtPosition(position);
            if (!TextUtils.isEmpty(selection)) {
                if (selection.equals(getString(R.string.gender_male))) {
                    mGender = PetsEntry.GENDER_MALE; // Male
                } else if (selection.equals(getString(R.string.gender_female))) {
                    mGender = PetsEntry.GENDER_FEMALE; // Female
                } else {
                    mGender = PetsEntry.GENDER_UNKNWON; // Unknown
                }
            }
        }

        // Because AdapterView is an abstract class, onNothingSelected must be defined
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            mGender = 0; // Unknown
        }
    });
}