Example usage for android.widget AdapterView setSelection

List of usage examples for android.widget AdapterView setSelection

Introduction

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

Prototype

public abstract void setSelection(int position);

Source Link

Document

Sets the currently selected item.

Usage

From source file:com.example.joeroger.homework2.activity.WeatherActivity.java

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

From source file:gxu.software_engineering.market.android.activity.UpdateItemActivity.java

@Override
protected void onStart() {
    super.onStart();
    spinner.setAdapter(mAdapter);/*ww  w.j a va  2s  .c o m*/
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            spinner.setSelection(UpdateItemActivity.this.cid.intValue() - 1);
        }
    }, 100);
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            UpdateItemActivity.this.cid = id;
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            parent.setSelection(UpdateItemActivity.this.cid.intValue());
        }
    });
    getSupportLoaderManager().initLoader(0, null, this);
}

From source file:org.developfreedom.logmein.ui.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(org.developfreedom.logmein.R.layout.activity_main);

    networkEngine = NetworkEngine.getInstance(this);
    databaseEngine = DatabaseEngine.getInstance(this);

    preferences = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());

    button_login = (ImageButton) findViewById(org.developfreedom.logmein.R.id.button_login);
    button_login.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            login();//from   w  w w .  j a  v a  2  s.  c om
        }
    });

    button_logout = (ImageButton) findViewById(org.developfreedom.logmein.R.id.button_logout);
    button_logout.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            logout();
        }
    });

    button_web = (ImageButton) findViewById(org.developfreedom.logmein.R.id.button_web);

    button_del = (Button) findViewById(org.developfreedom.logmein.R.id.button_del);
    button_del.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (getSelectedUsername() == null) {
                Toast.makeText(MainActivity.this, "User List is empty", Toast.LENGTH_SHORT).show();
                return;
            }
            String username = spinner_user_list.getSelectedItem().toString();
            //                showDeleteDialog("Delete User", "Are you sure you want to delete " + username, "YES", "NO").show();
            ManagerUserServices managerUserServices = new ManagerUserServices(MainActivity.this);
            Dialog dialog = managerUserServices.delete(spinner_user_list.getSelectedItem().toString());
            dialogDismissUpdater(dialog, 0);
        }
    });

    button_add = (Button) findViewById(org.developfreedom.logmein.R.id.button_add);
    button_add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ManagerUserServices managerUserServices = new ManagerUserServices(MainActivity.this);
            Dialog dialog = managerUserServices.add(getLayoutInflater());
            dialogDismissUpdater(dialog, spinner_user_list.getCount());
        }
    });

    button_edit = (Button) findViewById(org.developfreedom.logmein.R.id.button_edit);
    button_edit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (getSelectedUsername() == null) {
                Toast.makeText(MainActivity.this, "User List is empty", Toast.LENGTH_SHORT).show();
                return;
            }
            LayoutInflater inflater = getLayoutInflater();
            ManagerUserServices managerUserServices = new ManagerUserServices(MainActivity.this);
            Dialog dialog = managerUserServices.update(spinner_user_list.getSelectedItem().toString(),
                    inflater);
            dialogDismissUpdater(dialog, spinner_user_list.getSelectedItemPosition());
        }
    });

    user_list = databaseEngine.userList();
    adapter = new ArrayAdapter<String>(this, org.developfreedom.logmein.R.layout.spinner_layout, user_list);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
    spinner_user_list = (Spinner) findViewById(org.developfreedom.logmein.R.id.spinner_user_list);
    spinner_user_list.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    spinnerUpdateFlag = false;
    //Recover saved position
    if (user_list.size() > 0) { //Crashes otherwise at first startup
        int saved_pos = preferences.getInt(SettingsActivity.KEY_CURRENT_USERNAME_POS, 0);
        spinner_user_list.setSelection(saved_pos % user_list.size());
    }

    spinner_user_list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long l) {
            // An item was selected. We can retrieve the selected item using
            // parent.getItemAtPosition(pos)
            Log.d("Main", "onSelect: calling updateHomescreenData");
            updateHomescreenData();
            parent.setSelection(pos);
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
            Log.d("Main", "onNothinSelect: calling updateHomescreenData");
            updateHomescreenData();
        }
    });

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(org.developfreedom.logmein.R.id.container, new PlaceholderFragment()).commit();
    }
    updateHomescreenData();

    //animations
    startAnimation();

    //FIXME: Not the best way to call login method
    String intentMethod = getIntent().getStringExtra("methodName");
    if (intentMethod != null && intentMethod.equals("login")) {
        login();
        finish();
    } else if (intentMethod != null && intentMethod.equals("logout")) {
        logout();
        finish();
    }

    boolean prefNeedPersistence = preferences.getBoolean(SettingsActivity.KEY_PERSISTENCE,
            SettingsActivity.DEFAULT_KEY_PERSISTENCE);
    boolean perfStartupLogin = preferences.getBoolean(SettingsActivity.KEY_STARTUP_LOGIN,
            SettingsActivity.DEFAULT_KEY_STARTUP_LOGIN);
    if (prefNeedPersistence) {
        startService(new Intent(this, LoginService.class));
    }
    if (perfStartupLogin)
        login();

}

From source file:org.mozilla.gecko.tests.BaseTest.java

/**
 * Gets the view in the tabs panel at the specified index.
 *
 * @return View at index//from  w  w w  .j ava  2  s  .c o m
 */
private View getTabViewAt(final int index) {
    final View[] childView = { null };

    final AdapterView<ListAdapter> view = getTabsLayout();

    runOnUiThreadSync(new Runnable() {
        @Override
        public void run() {
            view.setSelection(index);

            // The selection isn't updated synchronously; posting a
            // runnable to the view's queue guarantees we'll run after the
            // layout pass.
            view.post(new Runnable() {
                @Override
                public void run() {
                    // getChildAt() is relative to the list of visible
                    // views, but our index is relative to all views in the
                    // list. Subtract the first visible list position for
                    // the correct offset.
                    childView[0] = view.getChildAt(index - view.getFirstVisiblePosition());
                }
            });
        }
    });

    boolean result = waitForCondition(new Condition() {
        @Override
        public boolean isSatisfied() {
            return childView[0] != null;
        }
    }, MAX_WAIT_MS);

    mAsserter.ok(result, "list item at index " + index + " exists", null);

    return childView[0];
}

From source file:com.sentaroh.android.TaskAutomation.Config.ProfileMaintenanceTaskProfile.java

final static private void setEditTaskActionListener(final GlobalParameters mGlblParms, final Dialog dialog,
        final AdapterProfileList pfla, ListView pflv, final String curr_grp,
        final ArrayList<TaskActionEditListItem> adapter_act_list) {
    final Spinner spinnerActionProfile = (Spinner) dialog.findViewById(R.id.edit_profile_task_user_actionlist);
    CustomSpinnerAdapter adapterActionProfile = new CustomSpinnerAdapter(mGlblParms.context,
            R.layout.custom_simple_spinner_item);
    adapterActionProfile.setTextColor(Color.BLACK);
    ProfileMaintenance.setSpinnerActionProfile(mGlblParms, dialog, pfla, pflv, spinnerActionProfile,
            adapterActionProfile);/*w w  w.  j a  v a2  s.co  m*/
    if (adapterActionProfile.getCount() == 0)
        spinnerActionProfile.setEnabled(false);

    final Spinner spinnerBuiltinPrimitiveAction = (Spinner) dialog
            .findViewById(R.id.edit_profile_task_builtin_primitive_actionlist);
    CustomSpinnerAdapter adapterBuiltinPrimitiveAction = new CustomSpinnerAdapter(mGlblParms.context,
            R.layout.custom_simple_spinner_item);
    adapterBuiltinPrimitiveAction.setTextColor(Color.BLACK);
    ProfileMaintenance.setSpinnerBuiltinPrimitiveAction(mGlblParms, dialog, spinnerBuiltinPrimitiveAction,
            adapterBuiltinPrimitiveAction);
    if (adapterBuiltinPrimitiveAction.getCount() == 0)
        spinnerBuiltinPrimitiveAction.setEnabled(false);

    final Spinner spinnerBuiltinConditionalAction = (Spinner) dialog
            .findViewById(R.id.edit_profile_task_builtin_conditional_actionlist);
    CustomSpinnerAdapter adapterBuiltinConditionalAction = new CustomSpinnerAdapter(mGlblParms.context,
            R.layout.custom_simple_spinner_item);
    adapterBuiltinConditionalAction.setTextColor(Color.BLACK);
    ProfileMaintenance.setSpinnerBuiltinConditionalAction(mGlblParms, dialog, spinnerBuiltinConditionalAction,
            adapterBuiltinConditionalAction);
    if (adapterBuiltinConditionalAction.getCount() == 0)
        spinnerBuiltinConditionalAction.setEnabled(false);

    final Spinner spinnerBuiltinCancelAction = (Spinner) dialog
            .findViewById(R.id.edit_profile_task_builtin_cancel_actionlist);
    CustomSpinnerAdapter adapterBuiltinCancelAction = new CustomSpinnerAdapter(mGlblParms.context,
            R.layout.custom_simple_spinner_item);
    adapterBuiltinCancelAction.setTextColor(Color.BLACK);
    ProfileMaintenance.setSpinnerBuiltinCancelAction(mGlblParms, dialog, spinnerBuiltinCancelAction,
            adapterBuiltinCancelAction);
    if (adapterBuiltinCancelAction.getCount() == 0)
        spinnerBuiltinCancelAction.setEnabled(false);

    final Spinner spinnerBuiltinBlockAction = (Spinner) dialog
            .findViewById(R.id.edit_profile_task_builtin_block_actionlist);
    CustomSpinnerAdapter adapterBuiltinBlockAction = new CustomSpinnerAdapter(mGlblParms.context,
            R.layout.custom_simple_spinner_item);
    adapterBuiltinBlockAction.setTextColor(Color.BLACK);
    ProfileMaintenance.setSpinnerBuiltinBlockAction(mGlblParms, dialog, spinnerBuiltinBlockAction,
            adapterBuiltinBlockAction);
    if (adapterBuiltinBlockAction.getCount() == 0)
        spinnerBuiltinBlockAction.setEnabled(false);

    final ListView lv_act_list = (ListView) dialog.findViewById(android.R.id.list);
    mGlblParms.taskActionListAdapter = new AdapterTaskActionEditList(mGlblParms.context,
            R.layout.task_action_list_item, adapter_act_list);
    lv_act_list.setAdapter(mGlblParms.taskActionListAdapter);
    lv_act_list.setEnabled(true);
    lv_act_list.setSelected(true);

    lv_act_list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        final public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
            if (!mGlblParms.taskActionListAdapter.getItem(position).action.startsWith(BUILTIN_PREFIX)
                    && !curr_grp.equals(QUICK_TASK_GROUP_NAME)) {
                for (int i = 0; i < pfla.getDataListCount(); i++) {
                    if (pfla.getDataListItem(i).getProfileType().equals(PROFILE_TYPE_ACTION)
                            && pfla.getDataListItem(i).getProfileGroup().equals(curr_grp)
                            && pfla.getDataListItem(i).getProfileName()
                                    .equals(mGlblParms.taskActionListAdapter.getItem(position).action)) {
                        NotifyEvent ntfy = new NotifyEvent(mGlblParms.context);
                        ntfy.setListener(new NotifyEventListener() {
                            @Override
                            public void positiveResponse(Context c, Object[] o) {
                                updateTaskActionEditListDescription(mGlblParms, dialog, pfla, curr_grp,
                                        adapter_act_list);
                                mGlblParms.taskActionListAdapter.notifyDataSetChanged();
                            }

                            @Override
                            public void negativeResponse(Context c, Object[] o) {
                            }
                        });
                        ProfileMaintenanceActionProfile pmap = ProfileMaintenanceActionProfile.newInstance();
                        pmap.showDialog(mGlblParms.frgamentMgr, pmap, "EDIT", curr_grp,
                                pfla.getDataListItem(i));
                        pmap.setNotifyEvent(ntfy);
                        break;
                    }
                }
            }
        }
    });

    lv_act_list.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        final public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            createTaskActionListContextMenu(mGlblParms, dialog, curr_grp, arg2, pfla);
            return false;
        }
    });

    // Add user action?
    spinnerActionProfile.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        final public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            if (arg2 != 0) {
                TaskActionEditListItem taeli = new TaskActionEditListItem();
                taeli.action = (String) spinnerActionProfile.getSelectedItem();
                mGlblParms.taskActionListAdapter.add(taeli);
                updateTaskActionEditListDescription(mGlblParms, dialog, pfla, curr_grp, adapter_act_list);
                mGlblParms.taskActionListAdapter.notifyDataSetChanged();
                lv_act_list.setSelection(mGlblParms.taskActionListAdapter.getCount() - 1);
                arg0.setSelection(0);
            }
        }

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

    // Add builtin action primitive?
    spinnerBuiltinPrimitiveAction.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        final public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            if (arg2 != 0) {
                TaskActionEditListItem taeli = new TaskActionEditListItem();
                taeli.action = (String) spinnerBuiltinPrimitiveAction.getSelectedItem();
                mGlblParms.taskActionListAdapter.add(taeli);
                mGlblParms.taskActionListAdapter.notifyDataSetChanged();
                lv_act_list.setSelection(mGlblParms.taskActionListAdapter.getCount() - 1);
                arg0.setSelection(0);
            }
        }

        @Override
        final public void onNothingSelected(AdapterView<?> arg0) {
        }

    });

    // Add builtin action conditional?
    spinnerBuiltinConditionalAction.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        final public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            if (arg2 != 0) {
                TaskActionEditListItem taeli = new TaskActionEditListItem();
                taeli.action = (String) spinnerBuiltinConditionalAction.getSelectedItem();
                mGlblParms.taskActionListAdapter.add(taeli);
                mGlblParms.taskActionListAdapter.notifyDataSetChanged();
                lv_act_list.setSelection(mGlblParms.taskActionListAdapter.getCount() - 1);
                arg0.setSelection(0);
            }
        }

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

    // Add builtin action cancel?
    spinnerBuiltinCancelAction.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        final public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            if (arg2 != 0) {
                TaskActionEditListItem taeli = new TaskActionEditListItem();
                taeli.action = (String) spinnerBuiltinCancelAction.getSelectedItem();
                mGlblParms.taskActionListAdapter.add(taeli);
                mGlblParms.taskActionListAdapter.notifyDataSetChanged();
                lv_act_list.setSelection(mGlblParms.taskActionListAdapter.getCount() - 1);
                arg0.setSelection(0);
            }
        }

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

    // Add builtin action block?
    spinnerBuiltinBlockAction.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        final public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            if (arg2 != 0) {
                TaskActionEditListItem taeli = new TaskActionEditListItem();
                taeli.action = (String) spinnerBuiltinBlockAction.getSelectedItem();
                mGlblParms.taskActionListAdapter.add(taeli);
                mGlblParms.taskActionListAdapter.notifyDataSetChanged();
                lv_act_list.setSelection(mGlblParms.taskActionListAdapter.getCount() - 1);
                arg0.setSelection(0);
            }
        }

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

    final Spinner spinnerSelectAction = (Spinner) dialog.findViewById(R.id.edit_profile_task_select_action);
    CustomSpinnerAdapter adapterSelectAction = new CustomSpinnerAdapter(mGlblParms.context,
            R.layout.custom_simple_spinner_item);
    adapterSelectAction.setTextColor(Color.BLACK);
    ProfileMaintenance.setSpinnerSelectAction(mGlblParms, dialog, spinnerSelectAction, adapterSelectAction);
    spinnerSelectAction.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        final public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            spinnerActionProfile.setVisibility(Spinner.GONE);
            spinnerBuiltinPrimitiveAction.setVisibility(Spinner.GONE);
            spinnerBuiltinConditionalAction.setVisibility(Spinner.GONE);
            spinnerBuiltinCancelAction.setVisibility(Spinner.GONE);
            spinnerBuiltinBlockAction.setVisibility(Spinner.GONE);
            String act = (String) spinnerSelectAction.getSelectedItem();
            if (act.equals(mGlblParms.context.getString(R.string.msgs_edit_profile_action_select_user))) {
                spinnerActionProfile.setVisibility(Spinner.VISIBLE);
            } else if (act
                    .equals(mGlblParms.context.getString(R.string.msgs_edit_profile_action_select_primitive))) {
                spinnerBuiltinPrimitiveAction.setVisibility(Spinner.VISIBLE);
            } else if (act.equals(
                    mGlblParms.context.getString(R.string.msgs_edit_profile_action_select_conditional))) {
                spinnerBuiltinConditionalAction.setVisibility(Spinner.VISIBLE);
            } else if (act
                    .equals(mGlblParms.context.getString(R.string.msgs_edit_profile_action_select_cancel))) {
                spinnerBuiltinCancelAction.setVisibility(Spinner.VISIBLE);
            } else if (act
                    .equals(mGlblParms.context.getString(R.string.msgs_edit_profile_action_select_block))) {
                spinnerBuiltinBlockAction.setVisibility(Spinner.VISIBLE);
            }
        }

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

}

From source file:com.brandroidtools.filemanager.fragments.NavigationFragment.java

/**
 * Method that loads the files in the adapter.
 *
 * @param files The files to load in the adapter
 * @hide//from   w  w w.  j a  v a2  s  .c o m
 */
@SuppressWarnings("unchecked")
private void loadData(final List<FileSystemObject> files) {
    //Notify data to adapter view
    final AdapterView<ListAdapter> view = (AdapterView<ListAdapter>) getView()
            .findViewById(RESOURCE_CURRENT_LAYOUT);
    FileSystemObjectAdapter adapter = (FileSystemObjectAdapter) view.getAdapter();
    adapter.clear();
    adapter.addAll(files);
    adapter.notifyDataSetChanged();
    view.setSelection(0);
}

From source file:com.benefit.buy.library.http.query.AbstractAQuery.java

/**
 * Set selected item of an AdapterView./*from   ww w .j a v  a 2 s.co  m*/
 * @param position The position of the item to be selected.
 * @return self
 */
public T setSelection(int position) {
    if (view instanceof AdapterView) {
        AdapterView<?> alv = (AdapterView<?>) view;
        alv.setSelection(position);
    }
    return self();
}

From source file:com.androidquery.AQuery.java

/**
 * Set selected item of an AdapterView./*from ww  w  .j a v a 2  s .c om*/
 *
 * @param position The position of the item to be selected.
 * @return self
 */
public AQuery setSelection(int position) {

    if (view instanceof AdapterView) {
        AdapterView<?> alv = (AdapterView<?>) view;
        alv.setSelection(position);
    }

    return self();

}

From source file:com.androidquery.AbstractAQuery.java

/**
 * Set selected item of an AdapterView.//  www .  j a  v  a2  s.  c  o m
 *
 * @param position The position of the item to be selected.
 * @return self
 */
public T setSelection(int position) {

    if (view instanceof AdapterView) {
        AdapterView<?> alv = (AdapterView<?>) view;
        alv.setSelection(position);
    }

    return self();

}