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:pl.motyczko.scrollheadersample.DummyListFragment.java

/**
 * {@inheritDoc}// ww w  . j a v  a2 s . c o  m
 */
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // This is the header
    // Remember to substract one from the touched position
    final String movie = (String) parent.getItemAtPosition(position);
    Toast.makeText(getActivity(), movie, Toast.LENGTH_SHORT).show();
}

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

/**
 * @param inflater/*from   w ww .  j  a va  2s  .co  m*/
 * @param container
 * @param savedInstanceState
 * @return
 */
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    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.list_forecast);
    mForecastAdapter = new ForecastAdapter(getActivity(), null, 0);

    mListView.setAdapter(mForecastAdapter);

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Cursor cursor = (Cursor) parent.getItemAtPosition(position);

            if (cursor != null) {
                String locationSetting = Utility.getPreferredLocation(getContext());
                Uri data = WeatherContract.WeatherEntry.buildWeatherLocationWithDate(locationSetting,
                        cursor.getLong(COL_WEATHER_DATE));

                callback.onItemSelected(data);

                Intent intent = new Intent(getActivity(), DetailActivity.class);
                intent.setData(data);
                startActivity(intent);
            }
            mPosition = position;

        }
    });

    if (savedInstanceState != null && savedInstanceState.containsKey(EXTRA_POSITION)) {
        mPosition = savedInstanceState.getInt(EXTRA_POSITION);
    }

    return rootView;
}

From source file:com.vignesh.conf.MainActivity.java

@Override
protected void onResume() {
    super.onResume();
    List<String> list = new ArrayList<>();
    final ListView listView = (ListView) findViewById(R.id.info);
    Cursor cursor;/*from   ww  w .j a v  a  2s .c o  m*/
    long time = new Date().getTime();
    ContentResolver contentResolver = getContentResolver();
    Uri uri = CalendarContract.Calendars.CONTENT_URI;
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());
    String cal = sharedPreferences.getString(CALENDAR_NAME, getResources().getString(R.string.no_calendar));
    String selection = "((" + CalendarContract.Calendars.ACCOUNT_NAME + " = ?))";
    int hours_before = Integer.valueOf(sharedPreferences.getString("past_hours", "2"));
    int hours_future = Integer.valueOf(sharedPreferences.getString("future_hours", "24"));
    String[] selectionArgs = new String[] { cal };

    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(getApplicationContext(), "No calendar permission", Toast.LENGTH_LONG).show();
        finish();
        return;
    }
    if (ActivityCompat.checkSelfPermission(getApplicationContext(),
            Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(getApplicationContext(), "No calling permission", Toast.LENGTH_LONG).show();
        finish();
        return;
    }
    cursor = contentResolver.query(uri, CAL_PROJECTION, selection, selectionArgs, null);
    assert cursor != null;
    while (cursor.moveToNext()) {
        long calID = 0;

        // Get the field values
        calID = cursor.getLong(PROJECTION_ID_INDEX);

        Uri.Builder instanceUriBuilder = CalendarContract.Instances.CONTENT_URI.buildUpon();
        ContentUris.appendId(instanceUriBuilder, time - hours_before * 60 * 60 * 1000);
        ContentUris.appendId(instanceUriBuilder, time + hours_future * 60 * 60 * 1000);
        Uri instanceUri = instanceUriBuilder.build();
        String instanceSelection = "((" + CalendarContract.Instances.CALENDAR_ID + "= ?))";
        String[] instanceSelectionArgs = new String[] { "" + calID };
        Cursor instanceCursor = contentResolver.query(instanceUri, INSTANCE_PROJECTION, instanceSelection,
                instanceSelectionArgs, CalendarContract.Instances.BEGIN + " ASC");
        assert instanceCursor != null;
        while (instanceCursor.moveToNext()) {
            String title = instanceCursor.getString(1);
            Date begin = new Date(instanceCursor.getLong(2));
            Date end = new Date(instanceCursor.getLong(3));
            String loc = instanceCursor.getString(4);
            String desc = instanceCursor.getString(5);
            String full = title + "\n" + loc + "\n" + desc;
            Pattern passcodePattern = Pattern
                    .compile(sharedPreferences.getString(REGEX, "(\\d{8})[\\s\\t\\n#]"));
            Matcher passcodeMatcher = passcodePattern.matcher(full);
            String list_item = (title + "\n" + "START: " + begin + "\nEND: " + end + "\n");
            if (passcodeMatcher.find())
                list.add(list_item + "Conf code: " + passcodeMatcher.group().trim() + "\n");
        }
        instanceCursor.close();
    }
    cursor.close();
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.list_layout, list);
    assert listView != null;
    listView.setAdapter(arrayAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String s = (String) parent.getItemAtPosition(position);
            SharedPreferences sharedPreferences = PreferenceManager
                    .getDefaultSharedPreferences(getApplicationContext());
            String tel = sharedPreferences.getString(CONTACT_NUM,
                    getResources().getString(R.string.no_contact));
            Pattern pattern = Pattern.compile(sharedPreferences.getString(REGEX, "(\\d{8})[\\s\\t\\n#]"));
            Matcher matcher = pattern.matcher(s);
            if (matcher.find()) {
                if (!tel.equals(getResources().getString(R.string.no_contact))) {
                    String[] contact = tel.split(": ");
                    String suffix = sharedPreferences.getString(SUFFIX, "");
                    if (suffix != null)
                        try {
                            suffix = URLEncoder.encode(suffix, "UTF-8");
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    String prefix = sharedPreferences.getString(PREFIX, "");
                    if (prefix != null)
                        try {
                            prefix = URLEncoder.encode(prefix, "UTF-8");
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    Intent intent = new Intent(Intent.ACTION_CALL,
                            Uri.parse("tel:" + contact[1] + "%3B" + prefix + matcher.group() + suffix));

                    if (ActivityCompat.checkSelfPermission(MainActivity.this,
                            Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                        startActivity(intent);
                    }
                } else {
                    new AlertDialog.Builder(MainActivity.this).setTitle("No contact Selected!")
                            .setMessage("Please Select Conference Call number via Settings Menu").show();
                }
            }
        }
    });
}

From source file:com.microsoft.live.sample.hotmail.ContactsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_contacts);

    ListView lv = getListView();//from w w  w  . j  a  v  a 2s . com
    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Contact contact = (Contact) parent.getItemAtPosition(position);
            ViewContactDialog dialog = new ViewContactDialog(ContactsActivity.this, contact);
            dialog.setOwnerActivity(ContactsActivity.this);
            dialog.show();
        }
    });

    LinearLayout layout = new LinearLayout(this);
    Button newCalendarButton = new Button(this);
    newCalendarButton.setText("New Contact");
    newCalendarButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            CreateContactDialog dialog = new CreateContactDialog(ContactsActivity.this);
            dialog.setOwnerActivity(ContactsActivity.this);
            dialog.show();
        }
    });

    layout.addView(newCalendarButton);
    lv.addHeaderView(layout);

    mAdapter = new ContactsListAdapter(this);
    setListAdapter(mAdapter);

    LiveSdkSampleApplication app = (LiveSdkSampleApplication) getApplication();
    mClient = app.getConnectClient();
}

From source file:com.github.michalbednarski.intentslab.ActivityMonitorActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        final Parcelable[] recordedIntentsRaw = savedInstanceState.getParcelableArray("recordedIntents");
        if (recordedIntentsRaw != null) {
            mRecordedIntents = new Intent[recordedIntentsRaw.length];
            System.arraycopy(recordedIntentsRaw, 0, mRecordedIntents, 0, recordedIntentsRaw.length);
        }/*from  www  . j  ava 2  s  .  c  o  m*/
    } else if (sStagingIntents == null) {
        startRecording();
    }
    if (mRecordedIntents == null && sStagingIntents == null) {
        finish();
        return;
    }
    getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Object item = parent.getItemAtPosition(position);
            if (item instanceof Intent) {
                startActivity(new Intent(ActivityMonitorActivity.this, IntentEditorActivity.class)
                        .putExtra(IntentEditorActivity.EXTRA_INTENT, (Intent) item));
            }
        }
    });
    chooseAndSetAdapter();
}

From source file:de.quist.app.maps.example.LayersDemoActivity.java

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    // This is also called by the Android framework in onResume(). The map may not be created at
    // this stage yet.
    if (mMap != null) {
        setLayer((String) parent.getItemAtPosition(position));
    }/*  w w  w.  ja  v a  2 s. co m*/
}

From source file:com.replicaappforyou.app.MainActivity.java

/**
 * Callback method for when a coupon is clicked. A new share Intent is created with the
 * coupon title. The the user can select which app to share the content of the coupon with
 * @param parent The AdapterView where the click happened
 * @param view The view within the AdapterView that was clicked (this will be a mvi provided
 *             by the adapter)/*from w ww  . j av  a  2s  .  c  o m*/
 * @param position The position of the view in the adapter that was clicked
 * @param id The row ID of the item that was clicked
 */
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    //Find coupon that was clicked based off of position in adapter
    Coupon coupon = (Coupon) parent.getItemAtPosition(position);
    //Create the share Intent
    Intent shareIntent = ShareCompat.IntentBuilder.from(this).setText(getShareText(coupon))
            .setType("image/jpeg").setStream(coupon.mImageUri).setChooserTitle("Redeem using")
            .createChooserIntent();
    startActivity(shareIntent);
}

From source file:com.afg.MngProductContentProvider.Fragments.ListProduct_Fragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mList = getListView();/*from  www.j a v  a2  s. c om*/
    setListAdapter(mAdapter);

    mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
            Bundle potatoe = new Bundle();
            potatoe.putParcelable(PRODUCT_KEY, (Product) parent.getItemAtPosition(i));
            mCallback.showManageProduct(potatoe);
        }
    });

    mList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
            mItemParent = adapterView;
            mItemPos = i;

            popup = new PopupMenu(getContext(), view);
            popup.setGravity(Gravity.END);
            popup.getMenuInflater().inflate(R.menu.delete_menu, popup.getMenu());

            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    p = (Product) mItemParent.getItemAtPosition(mItemPos);
                    mCallback.showDeletePopUp(p);
                    return true;
                }
            });
            popup.show();
            return true;
        }
    });

    mFab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mCallback.showManageProduct(null);
        }
    });
}

From source file:com.example.android.justforus.MainActivity.java

/**
 * Callback method for a when a coupon is clicked. A new share intent is created with the
 * coupon title. Then the user can select which app to share the content of the coupon with.
 *
 * @param parent The AdapterView where the click happened.
 * @param view The view within the AdapterView that was clicked (this
 *            will be a view provided by the adapter).
 * @param position The position of the view in the adapter.
 * @param id The row id of the item that was clicked.
 *///from  w  w  w . j a  v  a  2s  . com
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // Find coupon that was clicked based off of position in adapter
    Coupon coupon = (Coupon) parent.getItemAtPosition(position);

    // Create share intent
    Intent shareIntent = ShareCompat.IntentBuilder.from(this).setText(getShareText(coupon))
            .setType("image/jpeg").setStream(coupon.mImageUri).setChooserTitle(getString(R.string.redeem_using))
            .createChooserIntent();
    startActivity(shareIntent);
}

From source file:com.ijuru.kumva.app.activity.SearchActivity.java

/**
 * Called when a list item is selected (either a definition or suggestion)
 *///from   w  w  w. ja  va 2  s.  co m
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Object item = parent.getItemAtPosition(position);
    if (item instanceof Entry) {
        Entry definition = (Entry) item;
        ((KumvaApplication) getApplication()).setCurrentEntry(definition);

        Intent intent = new Intent(getApplicationContext(), EntryActivity.class);
        startActivity(intent);
    } else if (item instanceof Suggestion) {
        Suggestion suggestion = (Suggestion) item;
        doSearch(suggestion.getText());
    }
}