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:com.money.manager.ex.account.AccountTransactionListFragment.java

private void loadAccountsInto(final Spinner spinner) {
    if (spinner == null)
        return;/*from   w  w  w. j a  v  a 2  s .  com*/

    AccountService accountService = new AccountService(getActivity());
    accountService.loadTransactionAccountsToSpinner(spinner);

    // e switching of accounts.
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            // switch account.
            Cursor cursor = (Cursor) adapterView.getItemAtPosition(i);
            Account account = new Account();
            account.loadFromCursor(cursor);

            int accountId = account.getId();
            switchAccount(accountId);

            // color the spinner text of the selected item.
            int spinnerItemTextColor = ContextCompat.getColor(getActivity(), R.color.material_grey_50);
            ((TextView) spinner.getSelectedView()).setTextColor(spinnerItemTextColor);
        }

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

        }
    });
}

From source file:org.openremote.android.console.AppSettingsActivity.java

/**
 * Auto discovery servers and add them in a list view.
 * Click a list item and make it as current server.
 * //from w  w  w.  j a v  a 2s .co m
 * @return the list view
 */
private ListView constructAutoServersView() {
    final ListView lv = new ListView(this);
    lv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 200));
    lv.setPadding(20, 5, 5, 10);
    lv.setBackgroundColor(0);
    lv.setCacheColorHint(0);
    lv.setItemsCanFocus(true);
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    final ArrayAdapter<String> serverListAdapter = new ArrayAdapter<String>(appSettingsView.getContext(),
            R.layout.server_list_item, new ArrayList<String>());
    lv.setAdapter(serverListAdapter);

    new IPAutoDiscoveryServer() {
        @Override
        protected void onProgressUpdate(Void... values) {
            if (progressLayout != null) {
                progressLayout.setVisibility(View.VISIBLE);
            }
        }

        @Override
        protected void onPostExecute(List<String> result) {
            int length = result.size();
            for (int i = 0; i < length; i++) {
                serverListAdapter.add(result.get(i));
            }
            if (length > 0) {
                lv.setItemChecked(0, true);
                currentServer = serverListAdapter.getItem(0);
                AppSettingsModel.setCurrentServer(AppSettingsActivity.this, currentServer);
            }
            if (progressLayout != null) {
                progressLayout.setVisibility(View.INVISIBLE);
            }
            requestPanelList();
            checkAuthentication();
            requestAccess();
        }
    }.execute((Void) null);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            currentServer = (String) parent.getItemAtPosition(position);
            AppSettingsModel.setCurrentServer(AppSettingsActivity.this, currentServer);
            requestPanelList();
            checkAuthentication();
            requestAccess();
        }
    });

    return lv;
}

From source file:com.nearnotes.NoteEdit.java

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
    mBodyText.requestFocus();/*  w w w .j  a  v  a  2  s  .  c  o m*/
    autoCompView.setSelection(0);
    tempPosition = position;
    location = (String) adapterView.getItemAtPosition(position);
    Geocoder find = new Geocoder(getActivity());
    try {
        List<Address> AddressList = find.getFromLocationName(location, 10);
        if (AddressList.size() > 0) {
            longitude = AddressList.get(0).getLongitude();
            latitude = AddressList.get(0).getLatitude();
            //if 1st provider does not have data, loop through other providers to find it.
            int count = 0;
            while (longitude == 0 && count < AddressList.size()) {
                longitude = AddressList.get(count).getLongitude();
                latitude = AddressList.get(count).getLatitude();
                count++;
            }
            autoCompView.setTextColor(getResources().getColor(R.color.deepgreen));
        } else {
            StringBuilder sb = new StringBuilder("http://www.nearnotes.com/geocode.php");
            sb.append("?reference=" + String.valueOf(referenceList.get(tempPosition)));
            new NetworkTask().execute(sb.toString());

        }
    } catch (IOException e) {
        Log.e(LOG_TAG, "Couldnt received coordinates");
        e.printStackTrace();
    }
}

From source file:com.kiwiteam.nomiddleman.TourPageActivity.java

/**
 * Adapter to select day, time and quantity of a tour
 * @param parent/*from  w  w  w . j a va  2s . co m*/
 * @param view
 * @param position
 * @param id
 */
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    int vId = parent.getId();
    switch (vId) {
    /*
     * If day is selected, search for times at current day.
     */
    case R.id.day:
        String day = parent.getItemAtPosition(position).toString();
        ArrayList<String> times = tour.getTourSessionsTime(day);

        tTime = (Spinner) findViewById(R.id.time);
        tAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, times);
        tAdapter.notifyDataSetChanged();

        tTime.setOnItemSelectedListener(this);
        tAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        tTime.setAdapter(tAdapter);
        break;
    /*
     * If time is selected, search for quantities on current day and time.
     */
    case R.id.time:
        String time = parent.getItemAtPosition(position).toString();
        ArrayList<Integer> quantities = tour.getTourSessionAvailability(tDay.getSelectedItem().toString(),
                time);

        tQty = (Spinner) findViewById(R.id.quantity);
        qAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, quantities);
        qAdapter.notifyDataSetChanged();

        qAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        tQty.setAdapter(qAdapter);
        break;
    }
}

From source file:org.openremote.android.console.AppSettingsActivity.java

/**
 * It contains a list view to display custom servers, 
 * "Add" button to add custom server, "Delete" button to delete custom server.
 * The custom servers would be saved in customServers.xml. If click a list item, it would be saved as current server.
 * /*  w ww .  j a v  a  2s .  c  o m*/
 * @return the linear layout
 */
private LinearLayout constructCustomServersView() {
    LinearLayout custumeView = new LinearLayout(this);
    custumeView.setOrientation(LinearLayout.VERTICAL);
    custumeView.setPadding(20, 5, 5, 0);
    custumeView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    ArrayList<String> customServers = new ArrayList<String>();
    initCustomServersFromFile(customServers);

    RelativeLayout buttonsView = new RelativeLayout(this);
    buttonsView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 80));
    Button addServer = new Button(this);
    addServer.setWidth(80);
    RelativeLayout.LayoutParams addServerLayout = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    addServerLayout.addRule(RelativeLayout.CENTER_HORIZONTAL);
    addServer.setLayoutParams(addServerLayout);
    addServer.setText("Add");
    addServer.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(AppSettingsActivity.this, AddServerActivity.class);
            startActivityForResult(intent, Constants.REQUEST_CODE);
        }

    });
    Button deleteServer = new Button(this);
    deleteServer.setWidth(80);
    RelativeLayout.LayoutParams deleteServerLayout = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    deleteServerLayout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    deleteServer.setLayoutParams(deleteServerLayout);
    deleteServer.setText("Delete");
    deleteServer.setOnClickListener(new OnClickListener() {
        @SuppressWarnings("unchecked")
        public void onClick(View v) {
            int checkedPosition = customListView.getCheckedItemPosition();
            if (!(checkedPosition == ListView.INVALID_POSITION)) {
                customListView.setItemChecked(checkedPosition, false);
                ((ArrayAdapter<String>) customListView.getAdapter())
                        .remove(customListView.getItemAtPosition(checkedPosition).toString());
                currentServer = "";
                AppSettingsModel.setCurrentServer(AppSettingsActivity.this, currentServer);
                writeCustomServerToFile();
            }
        }
    });

    buttonsView.addView(addServer);
    buttonsView.addView(deleteServer);

    customListView = new ListView(this);
    customListView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 200));
    customListView.setCacheColorHint(0);
    final ArrayAdapter<String> serverListAdapter = new ArrayAdapter<String>(appSettingsView.getContext(),
            R.layout.server_list_item, customServers);
    customListView.setAdapter(serverListAdapter);
    customListView.setItemsCanFocus(true);
    customListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    if (currentCustomServerIndex != -1) {
        customListView.setItemChecked(currentCustomServerIndex, true);
        currentServer = (String) customListView.getItemAtPosition(currentCustomServerIndex);
    }
    customListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            currentServer = (String) parent.getItemAtPosition(position);
            AppSettingsModel.setCurrentServer(AppSettingsActivity.this, currentServer);
            writeCustomServerToFile();
            requestPanelList();
            checkAuthentication();
            requestAccess();
        }

    });

    custumeView.addView(customListView);
    custumeView.addView(buttonsView);
    requestPanelList();
    checkAuthentication();
    requestAccess();
    return custumeView;
}

From source file:com.dsna.android.main.MainActivity.java

private BaseFragment selectFragment(int position) {
    BaseFragment baseFragment = null;/*from w  ww.  j a  v a 2  s .  co  m*/

    switch (position) {
    case CASE_ACCINFO:
        break;
    case CASE_NEWFEEDS:
        baseFragment = new NewFeedsFragment(feeds);
        break;
    case CASE_MESSAGE:
        List<ConversationElement> conversations = dbHelper.getAllConversations(mUsername);
        AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
                final ConversationElement item = (ConversationElement) parent.getItemAtPosition(position);
                String conversationName = item.getName();
                List<OneMessage> msgs = dbHelper.getAllMessages(mUsername, conversationName);
                mBaseFragment = new ConversationFragment(conversationName, msgs);
                openFragment(mBaseFragment);
            }

        };
        baseFragment = new MessageFragment(conversations, onItemClickListener);
        break;
    case CASE_CONTACT:
        baseFragment = new AddingContactFragment();
        break;
    case CASE_POSTSTATUS:
        this.changeSessionKey();
        baseFragment = new PostStatusFragment();
        break;
    /*            case CASE_HEADER:
        baseFragment = new HeaderFragment();
        break;
    case CASE_SHADOW:
        baseFragment = new ShadowFragment();
        break;
    case CASE_THUMBNAIL:
        baseFragment = new ThumbnailFragment();
        break;
    case CASE_CARD:
        baseFragment = new CardFragment();
        break;
    case CASE_CARD_EXPAND:
        baseFragment = new CardExpandFragment();
        break;
    case CASE_BIRTH:
        baseFragment = new BirthDayCardFragment();
        break;
    case CASE_GPLAY:
        baseFragment = new GPlayCardFragment();
        break;
    case CASE_STOCK:
        baseFragment = new StockCardFragment();
        break;
    case CASE_MISC:
        baseFragment = new MiscCardFragment();
        break;
    case CASE_CHG_VALUE:
        baseFragment = new ChangeValueCardFragment();
        break;
    case CASE_LIST_BASE:
        baseFragment = new ListBaseFragment();
        break;
    case CASE_LIST_BASE_INNER:
        baseFragment = new ListDifferentInnerBaseFragment();
        break;
    case CASE_LIST_EXPAND:
        baseFragment = new ListExpandCardFragment();
        break;
    case CASE_LIST_GPLAY:
        baseFragment = new ListGplayCardFragment();
        break;
    case CASE_LIST_GPLAY_UNDO:
        baseFragment = new ListGplayUndoCardFragment();
        break;
    case CASE_GRID_BASE:
        baseFragment = new GridBaseFragment();
        break;
    case CASE_GRID_GPLAY:
        baseFragment = new GridGplayFragment();
        break;
    case CASE_LIST_COLORS:
        baseFragment = new ListColorFragment();
        break;
    case CASE_CURSOR_LIST:
        baseFragment = new ListCursorCardFragment();
        break;
    case CASE_CURSOR_GRID:
        baseFragment = new GridCursorCardFragment();
        break;
    case CASE_LIST_GPLAY_CAB:
        baseFragment = new ListGplayCardCABFragment();
        break;
    case CASE_GRID_GPLAY_CAB:
        baseFragment = new GridGplayCABFragment();
        break;
    case CASE_OVERFLOW_ANIM:
        baseFragment = new OverflowAnimFragment();
        break;*/
    default:
        break;
    }

    return baseFragment;
}

From source file:tv.acfun.video.CommentsActivity.java

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    int count = mAdapter.getCount();
    if (position > count) {
        if (isreload) {
            mFootview.findViewById(R.id.list_footview_progress).setVisibility(View.VISIBLE);
            TextView textview = (TextView) mFootview.findViewById(R.id.list_footview_text);
            textview.setText(R.string.buffering);
            requestData(pageIndex, false);
        }/*from w w  w. java2  s.  c om*/
        return;
    }
    //        showBar(); //TODO: show input bar when selected comment
    Object o = parent.getItemAtPosition(position);
    if (o == null || !(o instanceof Comment))
        return;
    Comment c = (Comment) o;
    int quoteCount = getQuoteCount();
    removeQuote(mCommentText.getText());
    if (quoteCount == c.count)
        return; // ?
    String pre = ":#" + c.count;
    mQuoteSpan = new Quote(c.count);
    SpannableStringBuilder sb = SpannableStringBuilder.valueOf(mCommentText.getText());
    TextView tv = TextViewUtils.createBubbleTextView(this, pre);
    BitmapDrawable bd = (BitmapDrawable) TextViewUtils.convertViewToDrawable(tv);
    bd.setBounds(0, 0, bd.getIntrinsicWidth(), bd.getIntrinsicHeight());
    sb.insert(0, pre);
    mQuoteImage = new ImageSpan(bd);
    sb.setSpan(mQuoteImage, 0, pre.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sb.setSpan(mQuoteSpan, 0, pre.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sb.append("");
    mCommentText.setText(sb);
    mCommentText.setSelection(mCommentText.getText().length());
}

From source file:com.juick.android.ThreadFragment.java

public JuickMessage findReply(AdapterView<?> parent, int replyNo) {
    for (int q = 0; q < parent.getCount(); q++) {
        Object itemAtPosition = parent.getItemAtPosition(q);
        if (itemAtPosition instanceof JuickMessage) {
            JuickMessage maybeReplied = (JuickMessage) itemAtPosition;
            if (maybeReplied.getRID() == replyNo) {
                return maybeReplied;
            }//from  ww  w.  j a  v  a 2  s  . c  o  m
        }
    }
    return null;
}

From source file:com.imaginamos.taxisya.taxista.activities.RegisterDriverActivity.java

public void prepareSpinners() {

    // set spinner
    mCountrySpinner = (Spinner) findViewById(R.id.spinner_country);

    mDepartmentSpinner = (Spinner) findViewById(R.id.spinner_department);

    mCitiesSpinner = (Spinner) findViewById(R.id.spinner_city);

    ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item,
            countriesArray);// w w w .j a v a  2  s . c  o  m
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
    mCountrySpinner.setAdapter(spinnerArrayAdapter);

    mCountrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView parent, View view, int pos, long log) {

            Country item = (Country) parent.getItemAtPosition(pos);

            Log.v("UPDATE_DATA", "spinner country" + String.valueOf(item.getId() + " " + item.getName()));

            int departmentId = firstDepartment(item.getId());
            updateDepartment(item.getId());
            updateCities(departmentId);

        }

        public void onNothingSelected(AdapterView arg0) {

        }
    });

    mDepartmentSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView parent, View view, int pos, long log) {

            Department item = (Department) parent.getItemAtPosition(pos);

            int departmentId = item.getId();

            Log.v("UPDATE_DATA", "spinner department" + String.valueOf(item.getId() + " " + item.getName()));

            updateCities(departmentId);

        }

        public void onNothingSelected(AdapterView arg0) {

        }
    });

    mCitiesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView parent, View view, int pos, long log) {

            City item = (City) parent.getItemAtPosition(pos);

            Log.v("UPDATE_DATA", "spinner city" + String.valueOf(item.getId() + " " + item.getName()));

            mCityId = item.getId();

        }

        public void onNothingSelected(AdapterView arg0) {

        }
    });

    int countryId = countriesArray.get(0).getId();
    int departmentId = firstDepartment(countryId);

    Log.v("UPDATE_DATA", " ++++++++++++++++++++++++++++++ ");
    Log.v("UPDATE_DATA", " countryId: " + String.valueOf(countryId));
    Log.v("UPDATE_DATA", " departmentId: " + String.valueOf(departmentId));

    updateDepartment(countryId);

    updateCities(departmentId);

}

From source file:com.nnm.smsviet.MessageListActivity.java

/**
 * {@inheritDoc}//  w  w w  .j  a v  a2s  .  c o m
 */
public final boolean onItemLongClick(final AdapterView<?> parent, final View view, final int position,
        final long id) {
    final Context context = this;
    final Message m = Message.getMessage(this, (Cursor) parent.getItemAtPosition(position));
    final Uri target = m.getUri();
    final int read = m.getRead();
    final int type = m.getType();
    Builder builder = new Builder(context);
    builder.setTitle(R.string.message_options_);

    final Contact contact = this.conv.getContact();
    final String a = contact.getNumber();
    Log.d(TAG, "p: " + a);
    final String n = contact.getName();

    String[] items = this.longItemClickDialog;
    if (TextUtils.isEmpty(n)) {
        items[WHICH_VIEW_CONTACT] = this.getString(R.string.add_contact_);
    } else {
        items[WHICH_VIEW_CONTACT] = this.getString(R.string.view_contact_);
    }
    items[WHICH_CALL] = this.getString(R.string.call) + " " + contact.getDisplayName();
    if (read == 0) {
        items = items.clone();
        items[WHICH_MARK_UNREAD] = context.getString(R.string.mark_read_);
    }
    if (type == Message.SMS_DRAFT) {
        items = items.clone();
        items[WHICH_FORWARD] = context.getString(R.string.send_draft_);
    }
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onClick(final DialogInterface dialog, final int which) {
            Intent i = null;
            switch (which) {
            case WHICH_VIEW_CONTACT:
                if (n == null) {
                    i = ContactsWrapper.getInstance().getInsertPickIntent(a);
                    Conversation.flushCache();
                } else {
                    final Uri u = MessageListActivity.this.conv.getContact().getUri();
                    i = new Intent(Intent.ACTION_VIEW, u);
                }
                try {
                    MessageListActivity.this.startActivity(i);
                } catch (ActivityNotFoundException e) {
                    Log.e(TAG, "activity not found: " + i.getAction(), e);
                    Toast.makeText(MessageListActivity.this, "activity not found", Toast.LENGTH_LONG).show();
                }
                break;
            case WHICH_CALL:
                MessageListActivity.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("tel:" + a)));
                break;
            case WHICH_MARK_UNREAD:
                ConversationListActivity.markRead(context, target, 1 - read);
                MessageListActivity.this.markedUnread = true;
                break;
            case WHICH_REPLY:
                MessageListActivity.this
                        .startActivity(ConversationListActivity.getComposeIntent(MessageListActivity.this, a));
                break;
            case WHICH_FORWARD:
                int resId;
                if (type == Message.SMS_DRAFT) {
                    resId = R.string.send_draft_;
                    i = ConversationListActivity.getComposeIntent(MessageListActivity.this,
                            MessageListActivity.this.conv.getContact().getNumber());
                } else {
                    resId = R.string.forward_;
                    i = new Intent(Intent.ACTION_SEND);
                    i.setType("text/plain");
                    i.putExtra("forwarded_message", true);
                }
                CharSequence text = null;
                if (PreferencesActivity.decodeDecimalNCR(context)) {
                    text = Converter.convertDecNCR2Char(m.getBody());
                } else {
                    text = m.getBody();
                }
                i.putExtra(Intent.EXTRA_TEXT, text);
                i.putExtra("sms_body", text);
                context.startActivity(Intent.createChooser(i, context.getString(resId)));
                break;
            case WHICH_COPY_TEXT:
                final ClipboardManager cm = (ClipboardManager) context
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                if (PreferencesActivity.decodeDecimalNCR(context)) {
                    cm.setText(Converter.convertDecNCR2Char(m.getBody()));
                } else {
                    cm.setText(m.getBody());
                }
                break;
            case WHICH_VIEW_DETAILS:
                final int t = m.getType();
                Builder b = new Builder(context);
                b.setTitle(R.string.view_details_);
                b.setCancelable(true);
                StringBuilder sb = new StringBuilder();
                final String a = m.getAddress(context);
                final long d = m.getDate();
                final String ds = DateFormat.format(context.getString(R.string.DATEFORMAT_details), d)
                        .toString();
                String sentReceived;
                String fromTo;
                if (t == Calls.INCOMING_TYPE) {
                    sentReceived = context.getString(R.string.received_);
                    fromTo = context.getString(R.string.from_);
                } else if (t == Calls.OUTGOING_TYPE) {
                    sentReceived = context.getString(R.string.sent_);
                    fromTo = context.getString(R.string.to_);
                } else {
                    sentReceived = "ukwn:";
                    fromTo = "ukwn:";
                }
                sb.append(sentReceived + " ");
                sb.append(ds);
                sb.append("\n");
                sb.append(fromTo + " ");
                sb.append(a);
                sb.append("\n");
                sb.append(context.getString(R.string.type_));
                if (m.isMMS()) {
                    sb.append(" MMS");
                } else {
                    sb.append(" SMS");
                }
                b.setMessage(sb.toString());
                b.setPositiveButton(android.R.string.ok, null);
                b.show();
                break;
            case WHICH_DELETE:
                ConversationListActivity.deleteMessages(context, target, R.string.delete_message_,
                        R.string.delete_message_question, null);
                break;
            default:
                break;
            }
        }
    });
    builder.show();
    return true;
}